AlertDialog的几种简单表现方法

AlertDialog

对话框在程序中不是必备的,但是用好对话框能对我们编写的应用增色不少。采用对话框可以大大增加应用的友好性。比较常用的背景是:用户登陆、网络正在下载、下载成功或者失败的提示,还有,比如:短信来了、电池没电了等等,只要你想到的,能提高用户体验的,你都可以使用对话框。

首先在布局文件中添加几个按钮,当我们点击不同的按钮是会有不同的表现形式

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="点击回退按钮(AlertDialog-01)"
        android:textSize="30sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="33dp"
        android:text="AlertDialog-02(选择座位)" 
        android:onClick="onClick02"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="19dp"
        android:text="AlertDialog-03(找回密码)" 
        android:onClick="onClick03"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button2"
        android:layout_marginTop="36dp"
        android:text="AlertDialog-04(自定义view)" 
        android:onClick="onClick04"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_alignRight="@+id/button3"
        android:layout_below="@+id/button3"
        android:layout_marginTop="24dp"
        android:text="AlertDialog-05(自定义样式)" />

</RelativeLayout>

因为涉及到了自定义样式所以先写一个自定义的样式xml,这个完全可以按自己喜好来安排

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="26dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/progressBar1"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="14dp"
        android:layout_toRightOf="@+id/progressBar1"
        android:text="helloworld" />

</RelativeLayout>

接下来就是几种表现形式的具体写法了

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

public class MainActivity extends Activity implements OnClickListener{

    private Button btn04;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn04=(Button) findViewById(R.id.button4);
        btn04.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this, 
                AlertDialog.THEME_HOLO_DARK)
                 //R.style.MyCustomDialog01)
                .setTitle("温馨提示")
                .setMessage("是否切换到数据网络")
                .setPositiveButton("确定", MainActivity.this)
                .setNegativeButton("取消", MainActivity.this)
                .show();
            }
        });

    }
    public void onClick02(final View v){
        final String items[]=new String[]{"不限","硬座","软座"};
        new AlertDialog.Builder(this)
        //.setTitle("请选择")
        .setSingleChoiceItems(items,
        -1, //-1表示默认没有选中
        new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            ((Button)v).setText(items[which]);
            dialog.dismiss();//销毁对话框
        }}).show();//create,show
    }
    public void onClick03(final View v){
        final String items[]=new String[]{"phone","email"};
        new AlertDialog.Builder(this)
        .setItems(items,new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ((Button)v).setText(items[which]);
                dialog.dismiss();//销毁对话框
        }}).show();//create,show
    }

    public void onClick04(View v){
        //ImageView imageView=new ImageView(this);
        //imageView.setImageResource(R.drawable.ic_launcher);
        View cv=View.inflate(this, R.layout.dialog_view_01, null);
        new AlertDialog.Builder(this)
        .setView(cv)
        .show();
    }

    /**点击回退操作时执行此方法*/
    @Override
    public void onBackPressed() {
        //构建对话框对象(借助内部类Builder)
        AlertDialog dialog=
        new AlertDialog.Builder(this)
        .setIcon(R.drawable.ic_launcher)//return this
        .setTitle("提示")
        .setMessage("您确认要退出系统吗?")
        .setPositiveButton("确定",new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                MainActivity.super.onBackPressed();
            }
        })
        .setNegativeButton("取消", null)
        .create();
        //显示对话框
        dialog.show();
        //super.onBackPressed();
    }
    @Override
    public void onClick(DialogInterface dialog, int which) {
        if(which==AlertDialog.BUTTON_POSITIVE){
        Toast.makeText(getApplicationContext(), "切换到数据", 0).show();
        }

    }


}

之后运行工程,通过具体的观察即可对上述代码进行更形象的理解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值