当前位置:操作系统 > 安卓/Android >>

Android 程式开发:(一)详解活动 —— 1.4 显示普通对话框

有的时候,可能需要弹出一个对话框,以便从用户的输入来获取某些确认信息。这种情况下,可以重写Activity基类中的受保护方法(protected)onCreateDialog()。
1、创建一个工程:Dialog。
 
2、main.xml中的代码。
 
<?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中的代码。
 
package net.horsttnann.Dialog; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInte易做图ce; 
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 易做图 text...") 
                    .setPositiveButton("OK", 
                            new DialogInte易做图ce.OnClickListener() { 
                                public void onClick(DialogInte易做图ce dialog, 
                                        int whichButton) { 
                                    Toast.makeText(getBaseContext(), 
                                            "OK clicked!", Toast.LENGTH_SHORT) 
                                            .show(); 
                                } 
                            }) 
                    .setNegativeButton("Cancel", 
                            new DialogInte易做图ce.OnClickListener() { 
                                public void onClick(DialogInte易做图ce dialog, 
                                        int whichButton) { 
                                    Toast.makeText(getBaseContext(), 
                                            "Cancel clicked!", 
                                            Toast.LENGTH_SHORT).show(); 
                                } 
                            }) 
                    .setMultiChoiceItems(items, itemsChecked, 
                            new DialogInte易做图ce.OnMultiChoiceClickListener() { 
                                public void
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,