从自定义对话框中的编辑文本中获取值
Get value from edittext in custom dialog
我有一个自定义对话框和 2 个编辑文本
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
。即使编辑文本为空。
改变这个
if (editTextNameAddBlockNum.getText().toString().equals(null))
至
if (editTextNameAddBlockNum.getText().toString().equals(""))
试试这个
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();
}
}
});
我有一个自定义对话框和 2 个编辑文本
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
。即使编辑文本为空。
改变这个
if (editTextNameAddBlockNum.getText().toString().equals(null))
至
if (editTextNameAddBlockNum.getText().toString().equals(""))
试试这个
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();
}
}
});