如何在单击一个控件时清除其他字段

时间:2015-09-08 11:10:34

标签: java android

我的警告对话框中有两个单选按钮和一个编辑文本字段。

例如,如果我选择一个单选按钮,编辑文本中的数据必须清除,反之亦然

以下是我的代码: 请仔细研究并帮助我

public class MainActivity extends Activity {

    Button press;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        press =(Button)findViewById(R.id.press);
        press.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertFormElements();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /*
 * Show AlertDialog with some form elements.
 */
    public void alertFormElements() {

    /*
     * Inflate the XML view. activity_main is in
     * res/layout/form_elements.xml
     */
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View formElementsView = inflater.inflate(R.layout.form_elements,
                null, false);

        // You have to list down your form elements


        final RadioGroup genderRadioGroup = (RadioGroup) formElementsView
                .findViewById(R.id.genderRadioGroup);

        final EditText nameEditText = (EditText) formElementsView
                .findViewById(R.id.nameEditText);

        // the alert dialog
        new AlertDialog.Builder(MainActivity.this).setView(formElementsView)
                .setTitle("Form Elements")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {

                        String toastString = "";
                        /*
                     * Getting the value of selected RadioButton.
                     */
                        // get selected radio button from radioGroup
                        int selectedId = genderRadioGroup
                                .getCheckedRadioButtonId();

                        // find the radiobutton by returned id
                        RadioButton selectedRadioButton = (RadioButton) formElementsView
                                .findViewById(selectedId);

                        toastString += "Selected radio button is: "
                                + selectedRadioButton.getText() + "!\n";

                    /*
                     * Getting the value of an EditText.
                     */
                        toastString += "Name is: " + nameEditText.getText()
                                + "!\n";

                        Toast.makeText(getApplicationContext(), "Item Clicked:" + toastString, Toast.LENGTH_SHORT).show();

                        dialog.cancel();
                    }
                }).show();
    }


}

2 个答案:

答案 0 :(得分:0)

您可以使用EditText.setText()方法将编辑文本的文本设置为空字符串。看起来你甚至没有在你的代码中尝试过这个。

也许在寻求帮助之前先尝试一下这个问题?

答案 1 :(得分:0)

选中单选按钮时,编辑文本将会清除。

在alertFormElements功能中使用以下代码

     genderRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            nameEditText.setText("");
        }
    });