zoukankan      html  css  js  c++  java
  • 安卓中的对话框通知---简单的对话框入门

    当你的应用需要显示一个进度条或需要用户对信息进行确认时,可以使用对话框来完成。

    1、用一个按钮来进行测试,在layout文件中的activity_main.xml文件中添加一个Button按钮:

    <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" >
    
        <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_alignParentTop="true"
            android:onClick="openDialog"
            android:text="@string/text_dialog" />
    
    </RelativeLayout>
    


    2、MainActivity中的代码:

    package com.example.lession16_dialog;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    	}
    
    	public void openDialog(View v) {
    		new AlertDialog.Builder(this)
    				.setIcon(R.drawable.ic_launcher)
    				.setTitle("xxxx")
    				.setMessage("是否创建文件")
    				.setPositiveButton("确认", new DialogInterface.OnClickListener() {
    
    					@Override
    					public void onClick(DialogInterface dialog, int which) {
    						// 创建文件了
    						new AlertDialog.Builder(MainActivity.this).setMessage(
    								"文件已经被创建").show();
    					}
    				})
    				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    
    					@Override
    					public void onClick(DialogInterface dialog, int which) {
    						new AlertDialog.Builder(MainActivity.this)
    								.setMessage("您已经选择了取消的按钮,该文件不会被创建").create()
    								.show();
    
    					}
    				}).show();
    	}
    
    	public void test1() {
    		// 创建对话框对象
    		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    
    		// 设置对话框的标题
    		alertDialog.setTitle("XXXXX");
    		// 设置对话框中的内容
    		alertDialog.setMessage("消息");
    		// 显示对话框
    		alertDialog.show();
    	}
    
    	public void tes2() {
    		AlertDialog alertDialog = new AlertDialog.Builder(this)
    				.setTitle("xxxx").setMessage("xxxx").show();
    
    	}
    
    }
    


    注:MainActivity中的test1和test2分别是两种显示Dialog的方法,openDialog也是一种,只不过常用openDialog中的方法,不过还是要看大家的习惯了!

  • 相关阅读:
    hdu5119 DP
    poj3692 最大点权独立集/最大独立集
    poj2125 最小点权覆盖集
    二分图总结
    poj2531 Network Saboteur
    poj1573 Robot Motion
    poj2632 Crashing Robots
    poj1068
    动态规划的思考(三)
    代刷题目分类(三)
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3155423.html
Copyright © 2011-2022 走看看