Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在单击一个控件时清除其他字段_Java_Android - Fatal编程技术网

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

Java 如何在单击一个控件时清除其他字段,java,android,Java,Android,我的警报对话框中有两个单选按钮和一个编辑文本字段 例如,如果我选择一个单选按钮,编辑文本中的数据必须清除,反之亦然 以下是我的代码: 请调查一下,帮我解决这个问题 public class MainActivity extends Activity { Button press; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInst

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

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

以下是我的代码: 请调查一下,帮我解决这个问题

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();
    }


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


在请求帮助之前,请尝试解决此问题?

当选中单选按钮时,“编辑文本”将被清除

在alertFormElements函数中使用以下代码

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

什么情况?提问时请尽可能具体。