Android入门--弹出三个按钮的对话框

在GUI程序中,有时需要弹出对话框来提示一些信息。

这些对话框比一个独立的屏幕简单,在Android中弹出式对话框不同于表示一个屏幕的活动,它通常用于简单的功能处理。

对话框的父类是android.app.Dialog,通过构建类android.app.AlertDialog来实现弹出式对话框,可以使用AlertDialog.Builder和不同的参数来构建对话框。

效果如下图所示:

  

activity_forwarding.xml代码如下:


<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="com.example.forwarding.Forwarding" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/Btn01" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_marginTop="20dp"
        android:text="@string/Texts"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
</RelativeLayout>

Forwarding.java代码如下:


package com.example.forwarding;

import android.support.v7.app.ActionBarActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Forwarding extends ActionBarActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forwarding);

		final Button dialogButton = (Button) findViewById(R.id.button1);
		final TextView texts = (TextView) findViewById(R.id.textView1);

		dialogButton.setOnClickListener(new View.OnClickListener() {// 按键单击事件

					@Override
					public void onClick(View v) {

						// TODO Auto-generated method stub

						new AlertDialog.Builder(Forwarding.this)
								.setTitle("系统提示")
								// 设置对话框标题

								.setMessage("请确认所有数据都保存后再推出系统!")
								// 设置显示的内容
								
								//右边按钮
								.setPositiveButton("确定",
										new DialogInterface.OnClickListener() {// 添加确定按钮

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {// 确定按钮的响应事件

												// TODO Auto-generated method
												// stub

												//finish();
												texts.setText("结果:确认 ");
											}

										})
										//中间按钮
										.setNeutralButton("测试", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
	texts.setText("结果:测试 ");
}
})

								//左边按钮
								.setNegativeButton("返回",
										new DialogInterface.OnClickListener() {// 添加返回按钮

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {// 响应事件

												// TODO Auto-generated method
												// stub

												//Log.i("alertdialog", " 请保存数据!");
												
												texts.setText("结果:返回 ");
											}
										}).show();// 在按键响应事件中显示此对话框
					}
				});
	}

}



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值