我有一个带单选按钮的自定义对话框,我需要从打开的对话框中监听更改(如果有的话)并从中设置文本。由于我使用java完成了,我不知道如何使用onCheckedChangedListener。我怎么做 。我是android新手,我不太了解。请帮助我。
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 :(得分:0)
您可以提取单选按钮的状态并将其保存在本地变量中以供进一步使用。
请参阅优秀的开发者指南: https://developer.android.com/guide/topics/ui/dialogs.html
通读一次,您将弄清楚如何在自定义对话框中提取和保存各种元素的状态。
答案 1 :(得分:0)
您可以通过以下方式完成此操作:
public class Availability extends Activity implements OnClickListener,OnCheckedChangeListener {
....
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(group.getId())
{
}
}
您可以将RadioButtons放入RadioGroup。