从 OnClick 中关闭 AlertDialog.Builder
我正在尝试为用户弹出一个对话框,该对话框的主体中有两个按钮,底部有一个取消按钮。当用户单击两个按钮之一时,对话框将消失,点击“取消”只会取消对话框。取消部分工作正常,但我不知道如何手动关闭对话框。这是我的代码:
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.config_dialog,
(ViewGroup) findViewById(R.id.config_dialog));
Button connect = (Button) layout.findViewById(R.id.config_connect);
Button delete = (Button) layout.findViewById(R.id.config_delete);
alert = new AlertDialog.Builder(Configuration.this);
alert.setTitle("Profile");
connect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
trace("Connect" + Integer.toString(position));
toast("Connected");
SharedPreferences app_preferences =
PreferenceManager.getDefaultSharedPreferences(Configuration.this);
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("IP", fetch.get(position).IP);
editor.commit();
//Add dismiss here
}
});
delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
trace("Delete");
}
});
// Set layout
alert.setView(layout);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
当我尝试添加alert.dismiss() 时,Eclipse 给出了一个错误。 .dismiss() 也不会出现在警报的自动完成列表中。
I'm trying to make it so that a dialog pops up for users which has two buttons in the body and a cancel button at the bottom. When a user clicks one of the two buttons the dialog will disappear, and hitting cancel will just cancel out of the dialog. The cancel part works fine, but I can't figure out how to dismiss the dialog manually. Here's my code:
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.config_dialog,
(ViewGroup) findViewById(R.id.config_dialog));
Button connect = (Button) layout.findViewById(R.id.config_connect);
Button delete = (Button) layout.findViewById(R.id.config_delete);
alert = new AlertDialog.Builder(Configuration.this);
alert.setTitle("Profile");
connect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
trace("Connect" + Integer.toString(position));
toast("Connected");
SharedPreferences app_preferences =
PreferenceManager.getDefaultSharedPreferences(Configuration.this);
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("IP", fetch.get(position).IP);
editor.commit();
//Add dismiss here
}
});
delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
trace("Delete");
}
});
// Set layout
alert.setView(layout);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
When I try to add the alert.dismiss(), Eclipse gives me an error. .dismiss() also doesn't show up in alert's autocomplete list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
梅林的答案是正确的,应该被接受,但为了完整起见,我将发布一个替代方案。
问题是您试图关闭 AlertDialog.Builder 而不是 AlertDialog 的实例。这就是 Eclipse 不会为您自动完成该方法的原因。在 AlertDialog.Builder 上调用 create() 后,您可以关闭收到的 AlertDialog。
Merlin's answer is correct and should be accepted, but for the sake of completeness I will post an alternative.
The problem is that you are trying to dismiss an instance of AlertDialog.Builder instead of AlertDialog. This is why Eclipse will not auto-complete the method for you. Once you call create() on the AlertDialog.Builder, you can dismiss the AlertDialog that you receive as a result.
AlertDialog.Builder
最适合小型简单对话框而不是自定义对话框。处理自定义对话框的最简洁方法是将
AlertDialog
子类化为上下文(在本例中为您的活动)中的私有静态类。这是一个简化的示例:
AlertDialog.Builder
is best suited for small simple dialog boxes rather than custom dialogs.The cleanest way to handle custom dialogs is to subclass
AlertDialog
as a private static class in your context (in this case your activity).Here is a simplified example:
代码非常简单:
最后以按钮的操作为例:
例如使用自定义警报对话框:
Java代码,您可以创建一个对象AlertDialog:
代码示例XML:layout_test.xml
最后,调用关于活动:
The code is very simple:
finally in the action of button for example:
For example with a custom alertdialog:
Code on java, you could create a Object AlertDialog:
Code example XML: layout_test.xml
finally, call on Activity:
无需创建自定义类。只需创建对对话框的外部引用并使用它来显示/关闭。
下面是我使用 Builder 创建带有许多按钮的自定义对话框的示例:
在您的类中声明它:
在 onCreate() 中,设置您希望对话框显示的时间。就我而言,我有一个按钮:
它的外观如下:
There's no need to create a custom class. Just create an external reference to your Dialog and use it to show/dismiss.
Here's an example where I use Builder to create a custom Dialog with many buttons:
Declare it within you class:
In your onCreate(), set when you want your Dialog to show up. In my case, I have a button:
And here's how it looks: