我有一个带有正负按钮的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()设置为这两个按钮。
https://stackoverflow.com/questions/18352324
复制相似问题