2012-10-16 71 views
1

我有一個單選按鈕的自定義對話框,我需要從打開的對話框中聽取更改(如有),並從中設置文本。由於我已經完成了使用java,我不知道如何使用onCheckedChangedListener。我怎麼做 。我是新來的機器人,我不知道多少。請幫助我。自定義對話框的單選按鈕

public class Availability extends Activity implements OnClickListener { 
    ToggleButton override; 
    TextView status; 
    Button Overridestatus; 
    CharSequence [] choice = {"RemoteOffice","Office","Personel mobile","Home","Mobile","Temporary","Do not Disturb","Working Remotely","Non work day","Work Day"}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.availability); 
     override = (ToggleButton) findViewById(R.id.tbOver); 
     override.setOnClickListener(this); 
     status = (TextView) findViewById(R.id.tvstatus); 
     Overridestatus = (Button) findViewById(R.id.bstatus); 
     Overridestatus.setOnClickListener(this); 

    } 

    public void onClick(View view) { 
     // TODO Auto-generated method stub 
     switch (view.getId()) { 
     case R.id.tbOver: 

      if (override.isChecked()) { 

       status.setText("Override"); 
       Overridestatus.setText("Personal Mobile"); 

      } else { 
       status.setText("Currently"); 
       Overridestatus.setText("Use Schedule - Do Not Disturb"); 

      } 
      break; 

     case R.id.bstatus: 

      { 
       if (override.isChecked()){ 
       showDialog(0); 
       } 
      } 

      break; 
     } 
    } 
    @Override 
    protected Dialog onCreateDialog(int id) { 
     // TODO Auto-generated method stub 
     switch (id) { 
     case 0: 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setTitle("Select System Server"); 
      builder.setIcon(R.drawable.ic_btn_round_more_normal); 
      builder.setPositiveButton("OK", 
        new DialogInterface.OnClickListener() { 

         public void onClick(DialogInterface dialog, int id) { 
          // TODO Auto-generated method stub 
          Intent menu = new Intent(Availability.this,MenuScreen.class); 
          startActivity(menu); 

         } 
        }); 
      builder.setNegativeButton("CANCEL", 
        new DialogInterface.OnClickListener() { 

         public void onClick(DialogInterface dialog, int which) { 
          // TODO Auto-generated method stub 

         } 
        }); 

      builder.setSingleChoiceItems(choice, -1, 
        new DialogInterface.OnClickListener() { 

         public void onClick(DialogInterface dialog, int item) { 
          // TODO Auto-generated method stub 

         } 
        }); 

      builder.create(); 
      builder.show(); 
     } 
     return null; 

    } 

} 

回答

0

您可以提取單選按鈕的狀態並將它們保存在局部變量中以備後用。

請參閱優秀的開發商在這裏指導:通過它閱讀 https://developer.android.com/guide/topics/ui/dialogs.html

一次,你會找出如何提取和保存各種元素的狀態在您的自定義對話框。

+0

你也可以看看這個 http://stackoverflow.com/questions/12908902/custom-dialog-date-and-time-picker-with-radio-buttons – chitranna

0

你可以做到這一點通過以下方式:

public class Availability extends Activity implements OnClickListener,OnCheckedChangeListener { 
    .... 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
    switch(group.getId()) 
    { 
    } 
} 

你可以把單選按鈕在RadioGroup中。

+0

這很好,但我的單選按鈕當我打開一個自定義對話框並且需要後面的東西時顯示(我也有單選按鈕不會與它們混淆) – chitranna

+0

您是否有自定義對話框的活動? – STT