2012-12-19 63 views
4

enter image description here安卓:帮助在单选吐司

如果用户点击下一步按钮不选择它具有敬酒消息选项“请选择任意一个”否则应该转到下一屏幕。我曾经试过,但它不会去下一个屏幕,而是其显示土司“请选择任意一个”我的代码

public class Question1 extends Activity implements OnClickListener { 
Button vid, next; 
Typeface tp; 
TextView tv; 
RadioGroup rg; 
RadioButton rb; 
Button b; 
SharedPreferences sp; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.question1); 
    sp = getSharedPreferences("AppFile1", 0); 

    tv = (TextView) findViewById(R.id.tvQuestion1); 

    rg = (RadioGroup) findViewById(R.id.radioGroup1); 
    vid = (Button) findViewById(R.id.buttonQuestion1VIdeo); 
    next = (Button) findViewById(R.id.buttonQuestion1Next); 
    vid.setOnClickListener(this); 
    next.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.buttonQuestion1VIdeo: 
     Intent i = new Intent(Question1.this, VideoActivity.class); 
     startActivity(i); 
     break; 
    case R.id.buttonQuestion1Next: 
     if (next.isSelected()) { 
      int selectedId = rg.getCheckedRadioButtonId(); 
      rb = (RadioButton) findViewById(selectedId); 
      String s = rb.getText().toString(); 
      SharedPreferences.Editor ed = sp.edit(); 
      ed.putString("q1", s); 
      ed.commit(); 
      Intent ia = new Intent(Question1.this, Question2.class); 
      startActivity(ia); 

     } else { 
      Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show(); 
     } 

     break; 
    default: 
     break; 
    } 

} 

}

+0

调试和应用破发点,并获得价值和比较结果1 –

回答

4

尝试用:

if (rg.getCheckedRadioButtonId()!=-1) { 
     int selectedId = rg.getCheckedRadioButtonId(); 
     rb = (RadioButton) findViewById(selectedId); 
     String s = rb.getText().toString(); 
     SharedPreferences.Editor ed = sp.edit(); 
     ed.putString("q1", s); 
     ed.commit(); 
     Intent ia = new Intent(Question1.this, Question2.class); 
     startActivity(ia); 

    } else { 
     Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show(); 
    } 

的文件说,如果没有按钮被选中,getCheckedRadioButtonId()回报-1

返回此组中所选单选按钮的标识符。在空选择时,返回值为-1。

因此,如果您在该函数中除了-1以外的任何值,那么您有一个选定的按钮。

3

错误条件if (next.isSelected())

真实状况:正如你想确保选项必须选择:

boolean checked = ((RadioButton) view).isChecked(); 
3

您正在检查条件下的Next按钮本身的选择状态。 但是你想检查单选按钮的选择状态。 所以你必须给它:

if (rg.getCheckedRadioButtonId()!=-1) { 
      int selectedId = rg.getCheckedRadioButtonId(); 
      rb = (RadioButton) findViewById(selectedId); 
      String s = rb.getText().toString(); 
      SharedPreferences.Editor ed = sp.edit(); 
      ed.putString("q1", s); 
      ed.commit(); 
      Intent ia = new Intent(Question1.this, Question2.class); 
      startActivity(ia); 

     } else { 
      Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show(); 
     } 
+0

意味着它无法检查里面选择任何选项或不'RadioGroup'没有检查所有单选框 –

+0

@ρяσѕρєяK不,这也是可能的。 :) 我的错 。编辑 –

0

的情况会是这样

rb1 = (RadioButton)findViewById(R.id.rb1); 
rb2 = (RadioButton)findViewById(R.id.rb2); 
rb3 = (RadioButton)findViewById(R.id.rb3); 

if(rb1.isChecked()==false && rb2.isChecked()==false && rb3.isChecked()==false) 
{ 
    // Toast message is here 
} 
else 
{ 
    // Intent code is here 
}