gpt4 book ai didi

android - 在 Android Alert 中编辑文本

转载 作者:行者123 更新时间:2023-11-29 14:54:21 26 4
gpt4 key购买 nike

我试图在 Android 中显示带有两个编辑文本字段的警报。来源如下:

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 出现。我做错了什么?

谢谢!

最佳答案

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();

}

关于android - 在 Android Alert 中编辑文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9862168/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com