首页

android 对话框的使用(入门级示例)

java

2020-6-28

package com.example.lxp20181212_duihua;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
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 {
	Button btn1,btn2,btn3,btn4,btn5;
	AlertDialog.Builder builder1,builder2,builder3;
	ProgressDialog builder4;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn1 = (Button)findViewById(R.id.button1);
		btn2 = (Button)findViewById(R.id.button2);
		btn3 = (Button)findViewById(R.id.button3);
		btn4 = (Button)findViewById(R.id.button4);
		btn5 = (Button)findViewById(R.id.button5);
		btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO 自动生成的方法存根
				builder1 = new AlertDialog.Builder(MainActivity.this);
				builder1.setIcon(R.drawable.ic_launcher);
				builder1.setTitle("这是一个确认对话框");
				builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO 自动生成的方法存根
						setTitle("你点击了对话框上的确定按钮");
					}
				});
				builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO 自动生成的方法存根
						setTitle("你点击了对话框上的取消按钮");
					}
				});
				builder1.show();
			}
		});
		btn2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO 自动生成的方法存根
				builder2 = new AlertDialog.Builder(MainActivity.this);
				builder2.setIcon(R.drawable.ic_launcher);
				builder2.setTitle("这是一个详细实例对话框");
				builder2.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO 自动生成的方法存根
						setTitle("你点击了对话框上的确定按钮");
					}
				});
				builder2.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO 自动生成的方法存根
						setTitle("你点击了对话框上的取消按钮");
					}
				});
				builder2.setNeutralButton("详细", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO 自动生成的方法存根
						setTitle("你点击了对话框上的详细按钮");
					}
				});
				builder2.show();
			}
		});
		btn3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO 自动生成的方法存根
				builder3 = new AlertDialog.Builder(MainActivity.this);
				builder3.setIcon(R.drawable.ic_launcher);
			
				LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
				final View textEntryView = inflater.inflate(R.layout.text_entry, null);
				builder3.setView(textEntryView);
				builder3.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO 自动生成的方法存根
						setTitle("你点击了对话框上的确定按钮");
					}
				});
				builder3.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO 自动生成的方法存根
						setTitle("你点击了对话框上的取消按钮");
					}
				});
				builder3.show();
			}
		});
		btn4.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO 自动生成的方法存根
				builder4 = new ProgressDialog(MainActivity.this);
				builder4.setTitle("正在处理中");
				builder4.setMessage("等待.....");
				builder4.show();
			}
		});
		btn5.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO 自动生成的方法存根
				Toast.makeText(getApplicationContext(), "这是一个Toast按钮", Toast.LENGTH_LONG).show();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
lxp20181212_duihua/.classpath , 475
lxp20181212_duihua/.project , 854
lxp20181212_duihua/.settings/org.eclipse.jdt.core.prefs , 177
lxp20181212_duihua/AndroidManifest.xml , 902
lxp20181212_duihua/bin/AndroidManifest.xml , 902
lxp20181212_duihua/bin/classes.dex , 693944
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/BuildConfig.class , 369
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/MainActivity$1$1.class , 1196
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/MainActivity$1$2.class , 1196
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/MainActivity$1.class , 2004
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/MainActivity$2$1.class , 1196
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/MainActivity$2$2.class , 1196
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/MainActivity$2$3.class , 1196
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/MainActivity$2.class , 2142
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/MainActivity$3$1.class , 1196
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/MainActivity$3$2.class , 1196
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/MainActivity$3.class , 2317
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/MainActivity$4.class , 1154
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/MainActivity$5.class , 1086
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/MainActivity.class , 2060
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/R$attr.class , 376
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/R$dimen.class , 497
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/R$drawable.class , 443
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/R$id.class , 716
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/R$layout.class , 473
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/R$menu.class , 424
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/R$string.class , 508
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/R$style.class , 467
lxp20181212_duihua/bin/classes/com/example/lxp20181212_duihua/R.class , 793
lxp20181212_duihua/bin/dexedLibs/android-support-v4-c20e0143adef3fd5ba035a22eac7b83f.jar , 232610
lxp20181212_duihua/bin/jarlist.cache , 120
lxp20181212_duihua/bin/lxp20181212_duihua.apk , 281463
lxp20181212_duihua/bin/resources.ap_ , 42122
lxp20181212_duihua/bin/res/crunch/drawable-hdpi/ic_launcher.png , 5964
lxp20181212_duihua/bin/res/crunch/drawable-mdpi/ic_launcher.png , 3112
lxp20181212_duihua/bin/res/crunch/drawable-xhdpi/ic_launcher.png , 9355
lxp20181212_duihua/bin/res/crunch/drawable-xxhdpi/ic_launcher.png , 17889
lxp20181212_duihua/gen/com/example/lxp20181212_duihua/BuildConfig.java , 172
lxp20181212_duihua/gen/com/example/lxp20181212_duihua/R.java , 3046
lxp20181212_duihua/ic_launcher-web.png , 51394
lxp20181212_duihua/libs/android-support-v4.jar , 621451
lxp20181212_duihua/proguard-project.txt , 781
lxp20181212_duihua/project.properties , 563
lxp20181212_duihua/res/drawable-hdpi/ic_launcher.png , 7658
lxp20181212_duihua/res/drawable-mdpi/ic_launcher.png , 3777
lxp20181212_duihua/res/drawable-xhdpi/ic_launcher.png , 12516
lxp20181212_duihua/res/drawable-xxhdpi/ic_launcher.png , 24777
lxp20181212_duihua/res/layout/activity_main.xml , 1910
lxp20181212_duihua/res/layout/text_entry.xml , 1231
lxp20181212_duihua/res/menu/main.xml , 263
lxp20181212_duihua/res/values-sw600dp/dimens.xml , 203
lxp20181212_duihua/res/values-sw720dp-land/dimens.xml , 277
lxp20181212_duihua/res/values-v11/styles.xml , 334
lxp20181212_duihua/res/values-v14/styles.xml , 391
lxp20181212_duihua/res/values/dimens.xml , 220
lxp20181212_duihua/res/values/strings.xml , 235
lxp20181212_duihua/res/values/styles.xml , 697
lxp20181212_duihua/src/com/example/lxp20181212_duihua/MainActivity.java , 4708
没有账号? 忘记密码?

社交账号快速登录