1

我有一个带有单选按钮的自定义对话框,我需要从打开的对话框中监听更改(如果有的话)并从中设置文本。由于我使用 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;

    }

}
4

2 回答 2

0

您可以提取单选按钮的状态并将它们保存在本地变量中以供进一步使用。

请参考这里的优秀开发者指南: https ://developer.android.com/guide/topics/ui/dialogs.html

通读一遍,您将了解如何在自定义对话框中提取和保存各种元素的状态。

于 2012-10-16T06:21:01.250 回答
0

您可以通过以下方式执行此操作:

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

您可以将 RadioButtons 放在 RadioGroup 中。

于 2012-10-16T06:25:18.843 回答