zoukankan      html  css  js  c++  java
  • Android开发之使用活动显示对话框

    利用活动显示对话框,需要重写Activity中的onCreateDialog()方法,以此来显示一个对话框窗口。

    效果如下:

    实现代码如下:

    package com.example.dialog;
    
    import java.util.zip.CheckedInputStream;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.AlertDialog.Builder;
    import android.app.Dialog;
    import android.app.ProgressDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;
    
    public class DialogActivity extends Activity {
    
        CharSequence[] items = {"美女","世界美女","绝食美女"};
        boolean[] itemsChecked = new boolean[items.length];
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
        
        public void onClick(View v){
            showDialog(0);
        }
        @Override
        @Deprecated
        protected Dialog onCreateDialog(int id) {
            // TODO Auto-generated method stub
            switch (id) {
            case 0:
                Builder builder =  new AlertDialog.Builder(this);
                builder.setIcon(R.drawable.ic_launcher);
                builder.setTitle("This is a dialog with some text");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show();
                    }
                 }
                );
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show();
                    }
                 }
                );
                builder.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface Dialog, int which, boolean isChecked) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getBaseContext(),
                        items[which] + (isChecked ? " Checked!":" Unchecked!" ),
                        Toast.LENGTH_SHORT).show();
                    }
                }
                );
                return builder.create();
                
            }
            return null;
            
        }
        public void onClick2(View v){
            final ProgressDialog progressDialog =ProgressDialog.show(this, "progressbar", "please waiting...");
            new Thread(new Runnable() {
                
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        Thread.sleep(3000);
                        progressDialog.dismiss();
                    } catch (InterruptedException e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
  • 相关阅读:
    DevExpress RichEditControl 上下翻页功能 z
    DockManager 如何快速隐藏DockPanel z
    DevExpress SpreadSheet报表模板设置 z
    DocumentManager在标签位置显示气泡框 z
    C#,数据类型扩展 z
    [安卓] 6、列表之ArrayAdapter适配
    [安卓] 5、SeekBar拖动条
    [安卓] 4、CheckBox、RadioButton和Toast简单用法
    [安卓] 3、EditText使用小程序
    [安卓] 2、使用2中方法做按钮监听和图片按钮使用
  • 原文地址:https://www.cnblogs.com/JczmDeveloper/p/3654637.html
Copyright © 2011-2022 走看看