带有图像的单选按钮

时间:2015-05-24 01:52:35

标签: java android

如何使用图片制作RadioButton,问题如下。当我们回答问题时,图片也会在RadioButton中发生变化。因为当我已回答问题时,他们仍然使用相同的图像。

我的意思是,当我们已经回答了问题时,RadioButton处的图像也会随着问题的变化而改变。我将图像放在RadioButton xml,如何将图像放在RadioButton

是否有可能或者我必须全部改变?

String Question[]={"HAHA","HAHA2","HAHA3"};

String ans[]={"i1","12","i3"};

String opt[]={"i1","lali","uu","12","hehe","oo","i3","jj","cc"};

in opt[]我们放置图片还是必须全部更改?所以我想把图像放在字符串

    public class FamilyQuestion extends ActionBarActivity {

    TextView tv;
    Button btnNext;
    RadioGroup rg;
    RadioButton bt1,bt2,bt3;
    String Question[]={"HAHA","HAHA2","HAHA3"};
    String ans[]={"i1","12","i3"};
    String opt[]={"i1","lali","uu","12","hehe","oo","i3","jj","cc"};

    int flag=0;
    public static int mark,correct,wrong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fruit_question);

        tv=(TextView) findViewById(R.id.tvque);
        btnNext=(Button) findViewById(R.id.btnNext);
        rg = (RadioGroup) findViewById(R.id.rg);
        bt1 = (RadioButton) findViewById(R.id.btn1);
        bt2 = (RadioButton) findViewById(R.id.btn2);
        bt3 = (RadioButton) findViewById(R.id.btn3);

        tv.setText(Question[flag]);
        bt1.setText(opt[0]);
        bt2.setText(opt[1]);
        bt3.setText(opt[2]);

        Toast.makeText(this, "Negative Mark : " + Test.tbflag, 1000).show();
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioButton uans=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
                String ansText= uans.getText().toString();

                if (ansText.equalsIgnoreCase(ans[flag]))
                {
                    correct++;
                    flag++;
                }
                else
                {
                    wrong++;

                }

                if(flag<Question.length)
                {
                    tv.setText(Question[flag]);
                    bt1.setText(opt[flag*3]);
                    bt2.setText(opt[(flag*3)+1]);
                    bt3.setText(opt[(flag*3)+2]);
                }
                else
                {
                    if(Test.tbflag)
                    {
                        mark=correct-wrong;
                    }
                    else
                    {
                        mark=correct;
                    }
                    Intent in=new Intent(getApplicationContext(),ResultActivity.class);
                    in.putExtra("question", "family");
                    startActivity(in);
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_family_question, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:background="@color/material_deep_teal_500"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.app.aditya.pecs.FamilyQuestion">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Question"
    android:id="@+id/tvque"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="41dp">

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvQuestion"
        android:layout_centerHorizontal="true"
        android:id="@+id/rg">


        <RadioButton
            android:layout_width="378dp"
            android:layout_height="158dp"
            android:text=""
            android:drawableRight="@drawable/cherry"
            android:id="@+id/btn1"
            android:checked="false" />
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:text=""
            android:drawableRight="@drawable/cherry"
            android:id="@+id/btn2"

            android:checked="false" />

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:text=""
            android:drawableRight="@drawable/cherry"
            android:id="@+id/btn3"

            android:checked="false" />


    </RadioGroup>
</ScrollView>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next"
    android:id="@+id/btnNext"
    android:layout_centerVertical="true"
    android:layout_toEndOf="@+id/scrollView"
    android:layout_marginStart="69dp" />

结果:

https://drive.google.com/open?id=0B7WFgWDcapm6THBGeTRxWVpKMG8&authuser=0

0 个答案:

没有答案