步步为营_Android开发课[18]_用户界面之Dialog(对话框)

Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305


  • 主题:用户界面之Dialog(对话框)

Dialog(对话框)实例:

activity_main.xml源代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button 
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="简易对话框"
    />
    <Button 
        android:id="@+id/btn2"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="列表选择框"
    />
    <Button 
        android:id="@+id/btn3"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="第一种进度条框"
    />
    <Button 
        android:id="@+id/btn4"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="第二种进度条框"
    />

</LinearLayout>

MainActivity.java源代码:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

    private Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }
    private void init() {

        btn1 =(Button)findViewById(R.id.btn1);
        btn1.setOnClickListener(this);
        btn2 =(Button)findViewById(R.id.btn2);
        btn2.setOnClickListener(this);
        btn3 =(Button)findViewById(R.id.btn3);
        btn3.setOnClickListener(this);
        btn4 =(Button)findViewById(R.id.btn4);
        btn4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch(v.getId()){

        case R.id.btn1:
            AlertDialog.Builder builder0 = new AlertDialog.Builder(MainActivity.this);
            builder0.setIcon(R.drawable.ic_launcher);
            builder0.setTitle("对话框");
            builder0.setMessage("你是准备去打酱油吗?");
            //设定积极按钮事件
            builder0.setPositiveButton("确认", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {

                    Toast.makeText(MainActivity.this, "点击了--确认",
                             Toast.LENGTH_SHORT).show();
                }
            });
            //设定消极按钮事件
            builder0.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {

                    Toast.makeText(MainActivity.this, "点击了--取消",
                             Toast.LENGTH_SHORT).show();
                }
            });
            builder0.create().show();
            break;

        case R.id.btn2:
            final String[] mItems = { "item0", "item1", "itme2", "item3", "itme4",
                    "item5", "item6" };
            AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
            builder1.setTitle("列表选择框");
            builder1.setItems(mItems, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {

                    Toast.makeText(MainActivity.this, mItems[which],
                             Toast.LENGTH_SHORT).show();
                }
            });
            builder1.create().show();
            break;

        case R.id.btn3:
            ProgressDialog mProgressDialog = new ProgressDialog(MainActivity.this);
            mProgressDialog.setIcon(R.drawable.ic_launcher);
            mProgressDialog.setTitle("第一种进度条框");
            //设定进度条风格
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            //设定进度条最大值
            mProgressDialog.setMax(75);
            mProgressDialog.setButton("确认",new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {

                    Toast.makeText(MainActivity.this, "点击了--确认",
                             Toast.LENGTH_SHORT).show();
                }
            });

            mProgressDialog.setButton("取消",new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {

                    Toast.makeText(MainActivity.this, "点击了--取消",
                             Toast.LENGTH_SHORT).show();
                }
            });
            mProgressDialog.show();

            break;

        case R.id.btn4:
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setTitle("第二种进度条框");
            mProgressDialog.setMessage("正在读取中,请稍候。。。");
            mProgressDialog.setIndeterminate(true);
            mProgressDialog.setCancelable(true);
            mProgressDialog.show();
            break;

        }
    }   
}

运行结果:

技术分享

点击第一个按钮:

技术分享

点击第二个按钮:

技术分享

点击第三个按钮:

技术分享

点击第四个按钮:

技术分享

Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。