簡體   English   中英

單選按鈕未顯示正確的已檢查ID

[英]radiobutton not showing correct checked id

我必須同時檢查一個單選按鈕,如果在烤面包上單擊ab1,則要查看timeone的值。,我怎么看..我正在使用的是“ rab” + radiobutton.ischecked()方法的數量。 還可以嗎?還是應該使用其他方法?

public class Alarmsettings extends Activity {
RadioButton rab1,rab2,rab3,rab0;
RadioGroup rg;
Button ab1,ab2;
public  Integer timeone;
public Alarmsettings()
{

}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_layout);
   rab0=(RadioButton) findViewById(R.id.radio0);
    rab1=(RadioButton) findViewById(R.id.radio1);
    rab2=(RadioButton) findViewById(R.id.radio2);
    rab3=(RadioButton) findViewById(R.id.radio3);
        ab1=(Button) findViewById(R.id.button1);
        ab2=(Button) findViewById(R.id.button2);
        rg = (RadioGroup) findViewById(R.id.radioGroup1);

        if(rab0.isChecked())
            timeone=15;
else    if(rab1.isChecked())
timeone=30;


else    if(rab2.isSelected())
            timeone=45;
else    if(rab3.isChecked())
            timeone=60;


        ab1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


            Toast.makeText(getApplicationContext(),timeone.toString(), Toast.LENGTH_SHORT).show();

嘗試通過標志分配值,而不是直接分配

        if(rab0.isChecked())
        flag=1;
        else if(rab1.isChecked())
        flag=2;
        else if(rab2.isSelected())
        flag=3;
        else if(rab3.isChecked())
        flag=4;
        ab1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        if(flag == 1)
        timeone=15;
        else if(flag == 2)
        timeone=30;
        else if(flag == 3)
        timeone=45;
        else if(flag == 4)
        timeone=60;


        Toast.makeText(getApplicationContext(),timeone.toString(), Toast.LENGTH_SHORT).show();

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM