从自定义对话框中的edittext获取值

时间:2017-07-31 08:14:34

标签: android android-studio customdialog

我有一个自定义对话框和2个edittext

final Dialog add_block_dialog = new Dialog(BlockNumActivity.this);
            add_block_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            add_block_dialog.setContentView(R.layout.add_blocknum_dialog);

            final EditText editTextNameAddBlockNum = (EditText) add_block_dialog.findViewById(R.id.editTextNameAddBlockNum);
            EditText editTextContactAddBlockNum = (EditText) add_block_dialog.findViewById(R.id.editTextContactAddBlockNum);
            final EditText editTextNumberAddBlockNum = (EditText) add_block_dialog.findViewById(R.id.editTextNumberAddBlockNum);
            Button buttonAddAddBlockNum = (Button) add_block_dialog.findViewById(R.id.buttonAddAddBlockNum);

            buttonAddAddBlockNum.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (editTextNameAddBlockNum.getText().toString().equals(null)) {
                        Toast.makeText(getApplicationContext(),"plz insert a name",Toast.LENGTH_SHORT).show();
                    }
                    if (editTextNumberAddBlockNum.getText().toString().equals(null)) {
                        Toast.makeText(getApplicationContext(),"plz insert a number",Toast.LENGTH_SHORT).show();
                    }
                    if (!editTextNumberAddBlockNum.getText().toString().equals(null) && !editTextNumberAddBlockNum.getText().toString().equals(null) ) {
                        Toast.makeText(getApplicationContext(),"successfuly saved",Toast.LENGTH_SHORT).show();
                    }
                }
            });

            add_block_dialog.show();

但是当我点击按钮时,它始终会显示successfuly saved。即使编辑文本为空。

2 个答案:

答案 0 :(得分:0)

更改此

if (editTextNameAddBlockNum.getText().toString().equals(null))

if (editTextNameAddBlockNum.getText().toString().equals(""))

答案 1 :(得分:0)

试试这个

buttonAddAddBlockNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editTextNameAddBlockNum.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(),"plz insert a name",Toast.LENGTH_SHORT).show();
                }
                if (editTextNumberAddBlockNum.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(),"plz insert a number",Toast.LENGTH_SHORT).show();
                }
                if (!editTextNumberAddBlockNum.getText().toString().equals("") && !editTextNumberAddBlockNum.getText().toString().equals("") ) {
                    Toast.makeText(getApplicationContext(),"successfuly saved",Toast.LENGTH_SHORT).show();
                }
            }
        });