Getting a user String in a dialog popup
In my Android code, I inflate the GUI and store a开发者_开发问答ll the sub-elements in a different class. In that class, I want to add a method to read a user input from. I tried to follow something like in this link but no matter what I do, it boils down into copying a final value into a non final value. I though about creating another gui but couldn't fins a way to do so. Here is the method I have now:
private String setText(int id){
AlertDialog.Builder alert = new AlertDialog.Builder(this.show);
final EditText input = new EditText(this.show);
alert.setView(input);
String out;
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//@Override
public void onClick(DialogInterface dialog, int which) {
Editable value = input.getText();
out = value.toString();
// TODO Auto-generated method stub
}
});
}
}
Which I use to return a string into another method which set the value of the TextView
.
I tried to do the following trick:
private String setText(){
AlertDialog.Builder alert = new AlertDialog.Builder(this.show);
final EditText input = new EditText(this.show);
alert.setView(input);
String out;
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
input.setText("canceled");
}
});
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert.show();
String toSend = input.getText().toString();
this.maxT.setText(toSend);
return toSend;
maxT
is a TextView
field. The app simply place an empty string. I figure that I should wait until the AlertDialog
is closed, I'm searching for a way to do so.
Just set the id of your View and reference it:
private String setText(int id){
AlertDialog.Builder alert = new AlertDialog.Builder(this.show);
EditText input = new EditText(this.show);
input.setId("myInput");
alert.setView(input);
String out;
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//@Override
public void onClick(DialogInterface dialog, int which) {
EditText input = (EditText) dialog.findViewById("myInput");
Editable value = input.getText();
out = value.toString();
}
});
}
}
View: setId() API
Dialogs onClick: onClick() API
You can create a custom dialog and access the ID that way. If you want to use an alert dialog you can try this:
First create a layout with a single editText:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:text="Stateful"
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
Then inflate this layout as in:
AlertDialog.Builder builder= new AlertDialog.Builder(this);
LayoutInflater inflater= getLayoutInflater();
final View myView= inflater.inflate(R.layout.alert_dialog_text_entry, null);
builder.setTitle("About");
builder.setMessage(alertMessage+"Version: "+versionName);
builder.setView(myView);
AlertDialog alert= builder.create();
Good post by Tony Stark here and you can beautify your dialog by adding an icon .. Make sure you have the picture in your drawable folder.
builder.setTitle("Message Sent!").setCancelable(false).setNegativeButton("Close",new DialogInterface.OnClickListener()
{public void onClick(DialogInterface dialog, int id) {dialog.cancel();}});
AlertDialog alert = builder.create();
alert.setIcon(R.drawable.send);//call your image for your icon here
alert.show();
Example
精彩评论