标签:
简单对话框
先写个布局放一个按钮
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:id="@+id/btn1" android:text="点击显示" /> </LinearLayout>
然后去写监听事件
package com.example.deemo; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initEvent(); } private void initEvent(){//点击事件 findViewById(R.id.btn1).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { showDialog1(); } }); } private void showDialog1(){//显示确认对话框 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("确认dialog"); builder.setIcon(R.drawable.ic_launcher);//图标 builder.setMessage("提示内容"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {//确定按钮 @Override public void onClick(DialogInterface dialog, int arg1) { Toast.makeText(MainActivity.this, "点击确定", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {//取消按钮 @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(MainActivity.this, "点击取消", Toast.LENGTH_SHORT).show(); } }); AlertDialog dialog = builder.create();//获取dialog dialog.show();//显示dialog } }
顺便一说,实测顺序改变也不影响确定和取消的位置,左取消右确定。
标签:
原文地址:http://www.cnblogs.com/webgavin/p/5746346.html