Java – Android dialog box and EditText value?
•
Java
I have a problem saving the string value located in EditText
The dialog box shows that you can use edit text, OK, and cancel button
What I want to happen when I press the OK button is that the bar variable gets the string value from EditText
public void dialog(){ final Dialog dialog = new Dialog(myClass.this); dialog.setContentView(R.layout.mydialog); dialog.setTitle("I'm soo smart. S-M-R-T. Smart."); dialog.setCancelable(true); dialog.show(); Button okButton = (Button) dialog.findViewById(R.id.dialog_OK_BUTTON); okButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { try{ LayoutInflater factory = LayoutInflater.from(Inloggning.this); final View textEntryView = factory.inflate(R.layout.myDialog,null); final EditText barText= (EditText) textEntryView.findViewById(R.id.dialog_FOO); // this gets returned empty. bar= barText.getText().toString(); System.out.println("foo: "+bar); //call(); dialog.hide(); } catch(Exception e){ // do whatever nessesary. } } }); Button cancelButton = (Button) dialog.findViewById(R.id.dialogbtn_cancel); cancelButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { dialog.dismiss(); } }); }
Does anyone know anything about this?
Editor: This is the sample code The actual code does not have duplicate names on variables
2nd edit: delete duplicates
Solution
Check if bartext is null
What happens if it is declared from the onclick listener?
Maybe change:
final EditText barText= (EditText) textEntryView.findViewById(R.id.dialog_FOO);
To:
final EditText barText= (EditText) dialog.findViewById(R.id.dialog_FOO);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码