alertdialog对话框(Android对话框的使用)

admin 535 0

这篇文章给大家聊聊关于alertdialog对话框,以及Android对话框的使用对应的知识点,希望对各位有所帮助,不要忘了收藏本站哦。

一、如何使用 AlertDialog 的返回值

在大多数情况下,我需要用户进行选择,很多倍。(我做的东西和提高消息框为用户作出的选择,继续做其他的事(也许称为块))所以我写了一个常见的函数

public static void ShowMsgDialog(Context self,String title, String msg)

虽然它正确地响应用户的操作,但始终挂起(的手段同时单击按钮前,一个操作值都是可见的全局变量的值)是存在着可能还在消息框中的任何函数返回值并使用它像这样:

int ret= ShowMsgDialog(Context self,String title, String msg);

public class MainActivity extends Activity{

enum Answer{ YES, NO, ERROR};

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button1=(Button)findViewById(R.id.button1);

button1.setOnClickListener(new OnClickListener(){

// TODO Auto-generated method stub

ShowMsgDialog(MainActivity.this,"Information","you choice?");

Toast.makeText(MainActivity.this,"YOU CHOICED YES", Toast.LENGTH_LONG).show();

Toast.makeText(MainActivity.this,"YOU CHOICED NO", Toast.LENGTH_LONG).show();

Toast.makeText(MainActivity.this,"ERROR OCUS", Toast.LENGTH_LONG).show();

//int ret= ShowMsgDialog(MainActivity.this,"Information","you choice?");

public static void ShowMsgDialog(Context self,String title, String Msg){

AlertDialog.Builder dlgAlert= new AlertDialog.Builder(self);

dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int whichButton){

dlgAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which){

// TODO Auto-generated method stub

我不认为有任何方式要从这样的 alertDialog中获取值

int ret= ShowMsgDialog(Context self,String title, String msg);

因为的时候您将显示对话框按钮的 onClick()将会已经完成。

所以我建议使用另一种实现的方法。

因为要创建 alertDialog的方法里面有您的活动它是创建一个函数在您的活动像下面一样容易:

public void userChoosed(String choise){

Toast.makeText(MainActivity.this,"YOU CHOICED YES", Toast.LENGTH_LONG).show();

Toast.makeText(MainActivity.this,"YOU CHOICED NO", Toast.LENGTH_LONG).show();

dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int whichButton){

dlgAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which){

二、android 的 AlertDialog 对话框样式可以修改吗

Android提供了AlertDialog类可通过其内部类Builder轻松创建对话框窗口,但是没法对这个对话框窗口进行定制,为了修改 AlertDialog窗口显示的外观,解决的办法就是创建一个指定的 AlertDialog和 AlertDialog.Builder类。

我们希望将上面默认的对话框外观修改为如下图所示的新对话框风格:

可从资源或者字符串直接指定对话框标题

可从资源、字符串和自定义布局来设置对话框内容

该对话框使用一个定制的布局来输出内容,布局定义的id将用于访问标题 TextView,下面是定义文件:

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:orientation="vertical"

android:background="@drawable/header"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

style="@style/DialogText.Title"

android:background="@drawable/title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

android:orientation="vertical"

android:background="@drawable/center"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:layout_width="fill_parent"

android:layout_height="wrap_content"/>

android:orientation="horizontal"

android:background="@drawable/footer"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:id="@+id/positiveButton"

android:layout_marginTop="3dip"

android:layout_height="wrap_content"

android:singleLine="true"/>

android:id="@+id/negativeButton"

android:layout_marginTop="3dip"

android:layout_height="wrap_content"

android:singleLine="true"/>

</LinearLayout>

根节点 LinearLayout的宽度设置为fill_parent而最小的宽度是280dip,因此对话框的宽度将始终为屏幕宽度的 87.5%

根节点 LinearLayout的宽度设置为fill_parent而最小的宽度是280dip,因此对话框的宽度将始终为屏幕宽度的 87.5%

自定义的主题用于声明对话框是浮动的,而且使用自定义的背景和标题视图:

三、怎么设置alertdialog message显示位置

Android中Dialog的示例代码如下:

AlertDialog.Builder builder= new Builder(context);

builder.setTitle("请输入");//设置对话框标题

builder.setIcon(android.R.drawable.btn_star);//设置对话框标题前的图标

final EditText edit= new EditText(context);

3.将输入框赋值给Dialog,并增加确定取消按键

builder.setPositiveButton("确认", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which){

Toast.makeText(context,"你输入的是:"+ edit.getText().toString(), Toast.LENGTH_SHORT).show();

builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which){

Toast.makeText(context,"你点了取消", Toast.LENGTH_SHORT).show();

builder.setCancelable(true);//设置按钮是否可以按返回键取消,false则不可以取消

AlertDialog dialog= builder.create();//创建对话框

dialog.setCanceledOnTouchOutside(true);//设置弹出框失去焦点是否隐藏,即点击屏蔽其它地方是否隐藏

四、android 怎么使对话框(AlertDialog.Builder)自动消失

1、新建一个AlertDialog,并用Builder方法形成了一个对象链,通过一系列的设置方法,构造出我们需要的对话框,然后调用show方法显示出来,注意到Builder方法的参数 self

2、这个其实是Activity对象的引用,根据你所处的上下文来传入相应的引用就可以了。例如在onCreate方法中调用,只需传入this即可。

五、...利用Java反射技术阻止通过按钮关闭对话框(AlertDialog)

众所周知,AlertDialog类用于显示对话框。关于AlertDialog的基本用法在这里就不详细介绍了,网上有很多,读者可以自己搜索。那么本文要介绍的是如何随心所欲地控制AlertDialog。

现在我们来看看第一个需求:如果某个应用需要弹出一个对话框。当单击“确定“按钮时完成某些工作,如果这些工作失败,对话框不能关闭。而当成功完成工作后,则关闭对话框。当然,无论何程度情况,单击“取消”按钮都会关闭对话框。

这个需求并不复杂,也并不过分(虽然我们可以自己弄个Activity来完成这个工作,也可在View上自己放按钮,但这显示有些大炮打蚊子了,如果对话框上只有一行文本,费这么多劲太不值了)。但使用过AlertDialog的读者都知道,无论单击的哪个按钮,无论按钮单击事件的执行情况如何,对话框是肯定要关闭的。也就是说,用户无法控制对话框的关闭动作。实际上,关闭对话框的动作已经在Android SDK写死了,并且未给使用者留有任何接口。但我的座右铭是“宇宙中没有什么是不能控制的”。

既然要控制对放框的关闭行为,首先就得分析是哪些类、哪些代码使这个对话框关闭的。进入AlertDialog类的源代码。在AlertDialog中只定义了一个变量:mAlert。这个变量是AlertController类型。AlertController类是Android的内部类,在com.android.internal.app包中,无法通过普通的方式访问。也无法在Eclipse中通过按Ctrl键跟踪进源代码。但可以直接在Android源代码中找到AlertController.java。我们再回到AlertDialog类中。AlertDialog类实际上只是一个架子。象设置按钮、设置标题等工作都是由AlertController类完成的。因此,AlertController类才是关键。

找到AlertController.java文件。打开后不要感到头晕哦,这个文件中的代码是很多地。不过这么多代码对本文的主题也没什么用处。下面就找一下控制按钮的代码。

在AlertController类的开头就会看到如下的代码: View.OnClickListener mButtonHandler= new View.OnClickListener(){

if(v== mButtonPositive&& mButtonPositiveMessage!= null){

m= Message.obtain(mButtonPositiveMessage);

} else if(v== mButtonNegative&& mButtonNegativeMessage!= null){

m= Message.obtain(mButtonNegativeMessage);

} else if(v== mButtonNeutral&& mButtonNeutralMessage!= null){

m= Message.obtain(mButtonNeutralMessage);

}// Post a message so we dismiss after the above handlers are executed

mHandler.obtainMessage(ButtonHandler.MSG_DISMISS_DIALOG, mDialogInterface)

从这段代码中可以猜出来,前几行代码用来触发对话框中的三个按钮( Positive、 Negative和 Neutral)的单击事件,而最后的代码则用来关闭对话框(因为我们发现了 MSG_DISMISS_DIALOG、猜出来的)。

mHandler.obtainMessage(ButtonHandler.MSG_DISMISS_DIALOG, mDialogInterface)

.sendToTarget();上面的代码并不是直接来关闭对话框的,而是通过一个 Handler来处理,代码如下:

private static final class ButtonHandler extends Handler{

// Button clicks have Message.what as the BUTTON{1,2,3} constant

private static final int MSG_DISMISS_DIALOG= 1;

private WeakReference< DialogInterface> mDialog; public ButtonHandler(DialogInterface dialog){

mDialog= new WeakReference< DialogInterface>(dialog);

public void handleMessage(Message msg){

case DialogInterface.BUTTON_POSITIVE:

case DialogInterface.BUTTON_NEGATIVE:

case DialogInterface.BUTTON_NEUTRAL:

((DialogInterface.OnClickListener) msg.obj).onClick(mDialog.get(), msg.what);

((DialogInterface) msg.obj).dismiss();

从上面代码的最后可以找到((DialogInterface) msg.obj).dismiss();。现在看了这么多源代码,我们来总结一下对话框按钮单击事件的处理过程。在AlertController处理对话框按钮时会为每一个按钮添加一个onclick事件。而这个事件类的对象实例就是上面的mButtonHandler。在这个单击事件中首先会通过发送消息的方式调用为按钮设置的单击事件(也就是通过setPositiveButton等方法的第二个参数设置的单击事件),在触发完按钮的单击事件后,会通过发送消息的方式调用dismiss方法来关闭对话框。而在AlertController类中定义了一个全局的mHandler变量。在AlertController类中通过ButtonHandler类来对象来为mHandler赋值。因此,我们只要使用我们自己Handler对象替换ButtonHandler就可以阻止调用dismiss方法来关闭对话框。下面先在自己的程序中建立一个新的ButtonHandler类(也可叫其他的名)。

class ButtonHandler extends Handler

{ private WeakReference< DialogInterface> mDialog; public ButtonHandler(DialogInterface dialog)

mDialog= new WeakReference< DialogInterface>(dialog);

public void handleMessage(Message msg)

{ case DialogInterface.BUTTON_POSITIVE:

case DialogInterface.BUTTON_NEGATIVE:

case DialogInterface.BUTTON_NEUTRAL:

((DialogInterface.OnClickListener) msg.obj).onClick(mDialog

}我们可以看到,上面的类和AlertController中的ButtonHandler类很像,只是支掉了switch语句的最后一个case子句(用于调用dismiss方法)和相关的代码。

下面我们就要为AlertController中的mHandler重新赋值。由于mHandler是private变量,因此,在这里需要使用Java的反射技术来为mHandler赋值。由于在AlertDialog类中的mAlert变量同样也是private,因此,也需要使用同样的反射技术来获得mAlert变量。代码如下:

AlertDialog alertDialog= new AlertDialog.Builder( this)

public void onClick(DialogInterface dialog,

}).setNegativeButton("取消", new OnClickListener()

public void onClick(DialogInterface dialog, int which)

上面的对话框很普通,单击哪个按钮都会关闭对话框。下面在调用 show方法之前来修改一个 mHandler变量的值, OK,下面我们就来见证奇迹的时刻。 try

Field field= alertDialog1.getClass().getDeclaredField(" mAlert");

Object obj= field.get(alertDialog1);

field= obj.getClass().getDeclaredField(" mHandler");

//修改mHandler变量的值,使用新的ButtonHandler类

field.set(obj, new ButtonHandler(alertDialog1));

alertDialog.show();我们发现,如果加上try catch语句,单击对话框中的确定按钮不会关闭对话框(除非在代码中调用dismiss方法),单击取消按钮则会关闭对话框(因为调用了dismiss方法)。如果去了try…catch代码段,对话框又会恢复正常了。

虽然上面的代码已经解决了问题,但需要编写的代码仍然比较多,为此,我们也可采用另外一种方法来阻止关闭对话框。这种方法不需要定义任何的类。

这种方法需要用点技巧。由于系统通过调用dismiss来关闭对话框,那么我们可以在dismiss方法上做点文章。在系统调用dismiss方法时会首先判断对话框是否已经关闭,如果对话框已经关闭了,就会退出dismiss方法而不再继续关闭对话框了。因此,我们可以欺骗一下系统,当调用dismiss方法时我们可以让系统以为对话框已经关闭(虽然对话框还没有关闭),这样dismiss方法就失效了,这样即使系统调用了dismiss方法也无法关闭对话框了。

下面让我们回到AlertDialog的源代码中,再继续跟踪到AlertDialog的父类Dialog的源代码中。找到dismissDialog方法。实际上,dismiss方法是通过dismissDialog方法来关闭对话框的,dismissDialog方法的代码如下: private void dismissDialog(){

if(Config.LOGV) Log.v(LOG_TAG,

" [Dialog] dismiss: already dismissed, ignore");

if(Config.LOGV) Log.v(LOG_TAG,

" [Dialog] dismiss: not showing, ignore");

} mWindowManager.removeView(mDecor); mDecor= null;

该方法后面的代码不用管它,先看 if(!mShowing){…}这段代码。这个 mShowing变量就是判断对话框是否已关闭的。因此,我们在代码中通过设置这个变量就可以使系统认为对话框已经关闭,就不再继续关闭对话框了。由于 mShowing也是 private变量,因此,也需要反射技术来设置这个变量。我们可以在对话框按钮的单击事件中设置 mShowing,代码如下:

Field field= dialog.getClass()

.getSuperclass().getDeclaredField(

//将mShowing变量设为false,表示对话框已关闭

将上面的代码加到哪个按钮的单击事件代码中,哪个按钮就再也无法关闭对话框了。如果要关闭对话框,只需再将 mShowing设为 true即可。要注意的是,在一个按钮里设置了 mShowing变量,也会影响另一个按钮的关闭对话框功能,因此,需要在每一个按钮的单击事件里都设置 mShowing变量的值。本文来自CSDN博客,转载请标明出处: http://blog.csdn.net/nokiaguy/archive/2010/07/27/5770263.aspx

OK,本文到此结束,希望对大家有所帮助。