我使用AlertDialog.Builder显示一个对话框,提示用户输入密码,然后希望将该密码保存在首选项中,但是我不知道如何从警报对话框的输入法获得结果。
以下是我希望能够做的事情:
String result;
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("Please enter a password");
final EditText input = new EditText(this);
b.setView(input);
b.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int whichButton)
{
//I get a compile error here, it wants result to be final.
result = input.getText().toString();
}
});
b.setNegativeButton("CANCEL", null);
b.create().show();
但是,我愿意做一些事情,比如showDialog(int);
,然后使用onCreateDialog(int)
方法,以某种方式设置结果并以其他方法接收结果,但我不知道如何处理最后一部分。
发布于 2011-05-11 01:31:31
public class MyActivity extends Activity {
private String result;
void showDialog() {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("Please enter a password");
final EditText input = new EditText(this);
b.setView(input);
b.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
result = input.getText().toString();
}
});
b.setNegativeButton("CANCEL", null);
b.show();
}
}
发布于 2011-05-11 01:06:38
简化示例:
public interface TextListener {
void onPositiveResult(CharSequence text);
}
public static AlertDialog getTextDialog(Context ctx,
final TextListener listener) {
View view = LayoutInflater.from(ctx).inflate(R.layout.dialog, null);
final TextView tv = (TextView) view.findViewById(R.id.tv);
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setView(view);
//
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.onPositiveResult(tv.getText());
}
});
builder.setNegativeButton(android.R.string.cancel, null);
return builder.create();
}
-- EDIT --或者您可以尝试以下代码:
public class Main extends Activity {
private Button btn;
private String result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(0);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
final EditText input = new EditText(this);
return new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("Please enter a password")
.setView(input)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(getBaseContext(), input.getText().toString(), Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show();
}
}).create();
}
return null;
}
}
发布于 2014-11-26 08:59:29
https://stackoverflow.com/questions/5953644
复制相似问题