我正在使用警报生成器来获得来自android用户的确认,但我也需要将edittext放在警报生成器中来从用户那里获得一些数据。
发布于 2014-11-16 16:16:51
您需要创建自己的自定义对话框:
http://www.mkyong.com/android/android-custom-dialog-example/
https://www.udemy.com/blog/android-alertdialog-examples/
或者在谷歌上搜索"android自定义警报对话框“
发布于 2014-11-16 16:27:59
这是你如何完成任务的例子。
将焦点放在问题上的位置用‘-->’标记:
public void yourAlertDialog(int title, int message, int value, YourResultListener listener) {
final View v;
// inflate additional layout
--> v = LayoutInflater.from(this).inflate(R.layout.edit_text, null);
// find desired views in the inflated layout:
--> final EditText et = (EditText) v.findViewById(R.id.edit_text);
--> et.setText("Your text - it may be passed as parameter");
// generate AlertDialog and return result if OK is pressed
new AlertDialog.Builder(this)//
.setTitle(title)//
.setMessage(message)//
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
if (listener != null) {
//DO WHAT YOU NEED
}
}
})//
.setNegativeButton(android.R.string.cancel, null)//
--> .setView(v)// adding additional view
.show();
}请注意,这里的this是您的主要活动
发布于 2014-11-16 16:46:42
最近,我还需要在警报对话框中添加EditText,代码如下:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Message");
alert.setMessage("Your custom message!");
// Set an EditText view to get user input
final EditText input = new EditText(this);
input.setText("Default text for EditText");
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Get value of EditText
String value = input.getText().toString();
//Do whatever you want to do with EditText value
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();https://stackoverflow.com/questions/26955115
复制相似问题