我正在尝试在Android中显示两个edittext字段的提醒。来源如下:
public void UserPass(){
final SharedPreferences prefs=getSharedPreferences("PrefsPreferences",MODE_PRIVATE);
String user=prefs.getString("user", "");
String pass=prefs.getString("password", "");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("U/P");
builder.setCancelable(false);
final EditText input1 = new EditText(this);
final EditText input2 = new EditText(this);
builder.setView(input1);
builder.setView(input2);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//My code
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
只显示一个EditText。我做错了什么?
谢谢!
答案 0 :(得分:5)
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
public class DialogWithInputBox extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString().trim();
Toast.makeText(getApplicationContext(), value,
Toast.LENGTH_SHORT).show();
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
alert.show();
}
}
答案 1 :(得分:1)
第二个setView
调用会替换您在第一个调用中设置的EditText
。你不能用这种方式添加两个View
。而是创建一个LinearLayout
,同时添加input1
和input2
,然后将其添加到builder
。