我有一个带有正负按钮的AlertDialog。在AlertDialog布局中,我有EditText和两个按钮(btnAdd1,btnAdd2)。我想当用户点击按钮btnAdd1或btnAdd2时添加相同的文本到AlertDialog中的EditText (但没有关闭AlertDialog)。这是不是可以在AlertDialog中做,或者我只能使用对话框?
这是AlertDialog的布局(R.layout.prompt):
<LinearLayout>
<EditText
android:id="@+id/userInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnAdd1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bla" />
<Button
android:id="@+id/btnAdd2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bla" />
</LinearLayout>
这是源代码:
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.prompt, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptView);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//...
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();
我想从布局中访问btnAdd1和btnAdd2。将OnClickListener()设置为这两个按钮。
发布于 2013-08-21 17:07:32
下面的代码将从R.layout.prompt
扩展视图并将其设置为AlertDialog。将不使用positive
和negative
按钮。您可以为btnAdd1
和btnAdd2
设置onClick
行为
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.prompt, null);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
EditText userInput = (EditText) promptView.findViewById(R.id.userInput);
Button btnAdd1 = (Button) promptView.findViewById(R.id.btnAdd1);
Button btnAdd2 = (Button) promptView.findViewById(R.id.btnAdd2);
btnAdd1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// btnAdd1 has been clicked
}
});
btnAdd2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// btnAdd2 has been clicked
}
});
alertD.setView(promptView);
alertD.show();
发布于 2013-08-21 16:29:25
你想做的是;
alertD.show();
Button button = (Button)promptView.findViewById(R.id.buttonId);
button.setOnClickListener(....)
使用视图调用findViewById
,而不是activity,后者将在正在显示的布局中查找id。
发布于 2015-08-22 14:49:08
根据这种方法,我能够创建图像按钮,但如果我想取消或取消对话框上的取消按钮,那么我必须做的。
public static void alertDialogShow(final Context context,
final String resultMobile) {
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompt,
null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
userInput.setText(resultMobile);
userInput.setEnabled(false);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
}
});
https://stackoverflow.com/questions/18352324
复制相似问题