频道栏目
首页 > 程序开发 > 移动开发 > Android > 正文
Android新手入门教程(四):理解Activityの显示对话框
2012-03-24 09:50:53           
收藏   我要投稿

有的时候,可能需要弹出一个对话框,以便从用户输入来获取某些确认信息。这种情况下,可以重写Activity基类中的受保护方法(protected)onCreateDialog()。

    1.创建一个名为Dialog的工程。

    2.main.xml中的代码。


[java]
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
     
<Button 
    android:id="@+id/btn_dialog" 
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content" 
    android:text="Click to display a dialog" 
    android:onClick="onClick" /> 
  
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
<Button
    android:id="@+id/btn_dialog"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click to display a dialog"
    android:onClick="onClick" />
 
</LinearLayout>    3.DialogActivity.java中的代码。

[java]
package net.horsttnann.Dialog; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Toast; 
 
public class DialogActivity extends Activity { 
    CharSequence[] items = { "Google", "Apple", "Microsoft" }; 
    boolean[] itemsChecked = new boolean[items.length]; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    } 
 
    public void onClick(View v) { 
        showDialog(0); 
    } 
 
    @Override 
    protected Dialog onCreateDialog(int id) { 
        switch (id) { 
        case 0: 
            return new AlertDialog.Builder(this) 
                    .setIcon(R.drawable.ic_launcher) 
                    .setTitle("This is a dialog with some simple text...") 
                    .setPositiveButton("OK", 
                            new DialogInterface.OnClickListener() { 
                                public void onClick(DialogInterface dialog, 
                                        int whichButton) { 
                                    Toast.makeText(getBaseContext(), 
                                            "OK clicked!", Toast.LENGTH_SHORT) 
                                            .show(); 
                                } 
                            }) 
                    .setNegativeButton("Cancel", 
                            new DialogInterface.OnClickListener() { 
                                public void onClick(DialogInterface dialog, 
                                        int whichButton) { 
                                    Toast.makeText(getBaseContext(), 
                                            "Cancel clicked!", 
                                            Toast.LENGTH_SHORT).show(); 
                                } 
                            }) 
                    .setMultiChoiceItems(items, itemsChecked, 
                            new DialogInterface.OnMultiChoiceClickListener() { 
                                public void onClick(DialogInterface dialog, 
                                        int which, boolean isChecked) { 
                                    Toast.makeText( 
                                            getBaseContext(), 
                                            items[which] 
                                                    + (isChecked ? " checked!" 
                                                            : " unchecked!"), 
                                            Toast.LENGTH_SHORT).show(); 
 
                                } 
                            }).create(); 
        } 
        return null; 
    } 

package net.horsttnann.Dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class DialogActivity extends Activity {
    CharSequence[] items = { "Google", "Apple", "Microsoft" };
    boolean[] itemsChecked = new boolean[items.length];

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onClick(View v) {
        showDialog(0);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 0:
            return new AlertDialog.Builder(this)
                    .setIcon(R.drawable.ic_launcher)
                    .setTitle("This is a dialog with some simple text...")
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    Toast.makeText(getBaseContext(),
                                            "OK clicked!", Toast.LENGTH_SHORT)
                                            .show();
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    Toast.makeText(getBaseContext(),
                                            "Cancel clicked!",
                                            Toast.LENGTH_SHORT).show();
                                }
                            })
                    .setMultiChoiceItems(items, itemsChecked,
                            new DialogInterface.OnMultiChoiceClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which, boolean isChecked) {
                                    Toast.makeText(
                                            getBaseContext(),
                                            items[which]
                                                    + (isChecked ? " checked!"
                                                            : " unchecked!"),
                                            Toast.LENGTH_SHORT).show();

                                }
                            }).create();
        }
        return null;
    }
}
 
    4.按F11调试。点击按钮显示对话框,在CheckBox上面打勾,就是弹出一个Toast提示,显示选中物件的文本信息。点击“OK”或“Cancel”按钮会使对话框消失。

效果图:

  \


 

提示:


    想要显示对话框,首先要重写Activity基类中的onCreateDialog()方法:


[java]
@Override 
protected Dialog onCreateDialog(int id) { 
    // ...  

    @Override
    protected Dialog onCreateDialog(int id) {
        // ...
    }
    当调用showDialog()的时候,上面被重写的方法就被调用了:


[java]
public void onClick(View v) { 
    showDialog(0); 

    public void onClick(View v) {
        showDialog(0);
    }    这个创建对话框的onCreateDialog()方法是一个被Activity控制的回调函数,当调用showDialog()时,onCreateDialog()回调函数就被触发了。showDialog()方法接受一个Integer参数,用来识别到底要显示哪个对话框。一般情况下,使用switch语句去判断显示不同的对话框。

    想要创建一个对话框,还需要使用AlertDialog类的Builder构造器,设置不同的属性,比如图标、标题、按钮、单选框等等:


[java]
    @Override 
    protected Dialog onCreateDialog(int id) { 
        switch (id) { 
        case 0: 
            Builder builder = new AlertDialog.Builder(this); 
            builder.setIcon(R.drawable.ic_launcher); 
            builder.setTitle("This is a dialog with some simple text..."); 
            builder.setPositiveButton("OK", 
                    new DialogInterface.OnClickListener() { 
                        public void onClick(DialogInterface dialog, 
                                int whichButton) { 
                            Toast.makeText(getBaseContext(), "OK clicked!", 
                                    Toast.LENGTH_SHORT).show(); 
                        } 
                    }); 
 
            builder.setNegativeButton("Cancel", 
                    new DialogInterface.OnClickListener() { 
                        public void onClick(DialogInterface dialog, 
                                int whichButton) { 
                            Toast.makeText(getBaseContext(), "Cancel clicked!", 
                                    Toast.LENGTH_SHORT).show(); 
                        } 
                    }); 
 
            builder.setMultiChoiceItems(items, itemsChecked, 
                    new DialogInterface.OnMultiChoiceClickListener() { 
                        public void onClick(DialogInterface dialog, int which, 
                                boolean isChecked) { 
                            Toast.makeText( 
                                    getBaseContext(), 
                                    items[which] 
                                            + (isChecked ? " checked!" 
                                                    : " unchecked!"), 
                                    Toast.LENGTH_SHORT).show(); 
                        } 
                    }); 
            return builder.create(); 
        } 
        return null; 
    } 
 

摘自  horsttnann
 
点击复制链接 与好友分享!回本站首页
上一篇:Android新手入门教程(三):理解Activityの隐藏标题栏
下一篇:Android新手入门教程(五):理解Activityの不得不说的Context
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站