2012-03-22 28 views
1

我有以下的alertdialog代码:如何知道在alertdialog中单击确定按钮时单击哪个单选按钮?

AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this); 
    helpBuilder.setTitle("Options"); 
    helpBuilder.setMessage("Choose Your Option"); 

    LayoutInflater inflater = getLayoutInflater(); 
    View radioButtonLayout = inflater.inflate(R.layout.popuplayout, null); 
    byNameRadioButton = (RadioButton) findViewById(R.id.byname); 
    byIdRadioButton = (RadioButton) findViewById(R.id.byid); 
    helpBuilder.setView(radioButtonLayout); 
    helpBuilder.setPositiveButton("Ok", 
      new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 
        Toast.makeText(Tab1Activity.this, 
        "Ok button is clicked", Toast.LENGTH_LONG) 
        .show(); 


        } 


       } 
      }); 

    AlertDialog helpDialog = helpBuilder.create(); 
    helpDialog.show(); 

enter image description here

我想知道点击了哪个单选按钮被点击OK OK按钮时? 需要帮助。 Thanx!

回答

3

为什么不使用带有单选按钮的对话框的默认实现,您可以在这里找到:Dialogs。你不需要额外写任何代码,你就会得到你需要的东西! :) 这里是例子:

final CharSequence[] items = {"By Score", "By Name", "By Id"}; 

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Options"); 
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int item) { 
     Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 
    } 
}); 
AlertDialog alert = builder.create(); 
+1

没错,默认版本是比较有用的,那么你用OK按钮。 – asktomsk 2012-03-22 09:33:50

+0

为什么没有这个代码工作? okButton =(Button)findViewById(R.id.ok); \t \t okButton.setOnClickListener(新View.OnClickListener(){ \t \t \t公共无效的onClick(视图v){// 上面的代码ü提 \t \t \t \t \t \t \t} \t \t} );' 点击oK按钮,alertdialog应该出现,但没有任何反应。 – captaindroid 2012-03-22 09:44:35

+0

确切的代码我写道: 'okButton.setOnClickListener(新View.OnClickListener(){ 公共无效的onClick(视图v){ 最终的CharSequence []项目= { “ByScore”, “绰号”, “ById”}; \t \t \t \t AlertDialog.Builder助洗剂=新AlertDialog.Builder(Tab1Activity.this); \t \t \t \t builder.setTitle( “选项”); \t \t \t \t builder.setSingleChoiceItems(项,-1, \t \t \t \t \t \t新DialogInterface.OnClickListener(){ \t \t \t \t \t \t \t公共无效的onClick(DialogInterface对话,诠释项){ \t \t \t \t \t \t \t \t Toast.makeText(getApplicationContext(), \t \t \t \t \t \t \t \t \t \t it ems [item],Toast.LENGTH_SHORT).show(); \t \t \t \t \t \t \t} \t \t \t \t \t \t}); AlertDialog alert = builder。创建(); }}); ' – captaindroid 2012-03-22 09:48:19

相关问题