でも、リリース後にどのようにボタンを作ることを選択したまま?

ニック・K:

私は、ユーザーがそれを選択した後にクリックされたボタンの滞在をしようとしています。私のプログラムは、ユーザーがオプションを選択し、その質問に戻る場合は選択したとして、それが表示されるべき10の質問クイズです。

私は、クリックされたような質問を切り替える際にボタンを表示する方法を見つけ出すように見えることはできません。

mAButton.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {
            Log.i(TAG, "A Button Clicked");
            answers[questionIndex] = "Question: " + questionIndex + ", Answer: A";
            Toast t;
            t = Toast.makeText(MainActivity.this, R.string.recorded_toast, Toast.LENGTH_SHORT);
            t.show();
        }
});

これは私が答え(私は答えのために5つのボタンを持っている)のいずれかに持っているのonClickの一例です。私は、ユーザーがこの回答を選択した場合、彼らは別の答えを選んだまでは、ボタンがクリックされたままになりますようにそれをしたいです。私はオンライン私が見てきたいくつかのことを試してみたが、何も今のところうまく機能しません。

編集:各質問には5つの答えと複数の選択肢です!

ザイン:

単一の回答は回答のリストの間で確認することが許可されている場合は、使用する必要があるラジオボタンを

複数回答を確認することが許可されている場合は、使用する必要があるのCheckBoxまたはトグルボタンを

ここでクリックされたとして、ボタンをシミュレート使用してトグルボタンの抜粋です

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:textOff="Unselected"
        android:textOn="Selected" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:textOff="Unselected"
        android:textOn="Selected" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:textOff="Unselected"
        android:textOn="Selected" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:textOff="Unselected"
        android:textOn="Selected" />
</LinearLayout>

背景選択のための描画可能を作成

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/checked_button" android:state_checked="true" />
    <item android:drawable="@color/unchecked_button" android:state_checked="false" />
</selector>

<resources>
    <?xml version="1.0" encoding="utf-8"?>
    ...
    <color name="checked_button">#989898</color>
    <color name="unchecked_button">#f8f8f8</color>

</resources>

Javaの:

ToggleButton button1 = findViewById(R.id.btn1);
ToggleButton button2 = findViewById(R.id.btn2);
ToggleButton button3 = findViewById(R.id.btn3);
ToggleButton button4 = findViewById(R.id.btn4);

button1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // Button1 is checked
        } else {
            // Button1 is unchecked
        }
    }
});

ここでは、画像の説明を入力します。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=206197&siteId=1