2012-02-23 59 views
0

我有一个Android应用程序,我想向用户提供使用不同字段排序表的选择。我使用了一个带有三个单选按钮的对话框来提供每种排序类型的选择。在Android应用程序的对话框中使用单选按钮

下面的对话框代码中的代码声明单选按钮

 LayoutInflater layoutInflater = (LayoutInflater) 
     getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final View layout = layoutInflater.inflate(R.layout.lectindex_dialog, (ViewGroup) findViewById(R.id.lect_index)); 

     builder.setView(layout); 

     // Now configure the AlertDialog 
     builder.setTitle(R.string.exindex_title);      

     final RadioButton radio_date = (RadioButton) findViewById(R.id.RBdate); 
     final RadioButton radio_loctn = (RadioButton) findViewById(R.id.RBloctn); 
     final RadioButton radio_stream = (RadioButton) findViewById(R.id.RBstream);   
     radio_date.setOnClickListener(radio_listener); 
     radio_loctn.setOnClickListener(radio_listener); 
     radio_stream.setOnClickListener(radio_listener); 

     builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       // We forcefully dismiss and remove the Dialog, so it cannot be used again (no cached info) 
       LecturesActivity.this.removeDialog(SELINDEX_DIALOG_ID); 
      } 
     }); 

为radio_listener的代码另行申报,像这样

 private OnClickListener radio_listener = new OnClickListener() { 
      public void onClick(View v) { 
      // Perform action on clicks 
      RadioButton rb = (RadioButton) v; 
      Toast.makeText(LecturesActivity.this, rb.getText(), Toast.LENGTH_SHORT).show(); 
      } 
     }; 

一切似乎工作确定,但对于radio_listener代码永远不会被使用,为什么?

回答

0

错误和radio_listener永远不会被调用的原因是它在尝试使用radio_date时会引发错误,因为它没有正确声明。当我将声明更改为最终RadioButton radio_date =(RadioButton)layout.findViewById(R.id.RBdate); 其中布局是对话框的代码,代码完美工作。

0

您是否尝试过使用RadioGroup而不是单独的单选按钮?您将更少地控制自己的行为IMO

相关问题