MainActivity.java
activity_main.xml
custom.xml
package com.simpleDialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button btn_simple, btn_custom, btn_progress, btn_single, btn_multi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_simple = (Button) findViewById(R.id.btn_simple);
btn_custom = (Button) findViewById(R.id.btn_custom);
btn_progress = (Button) findViewById(R.id.btn_progress);
btn_single = (Button) findViewById(R.id.btn_single);
btn_multi = (Button) findViewById(R.id.btn_multi);
btn_simple.setOnClickListener(this);
btn_custom.setOnClickListener(this);
btn_progress.setOnClickListener(this);
btn_single.setOnClickListener(this);
btn_multi.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_simple:
showDialog(R.id.btn_simple);
break;
case R.id.btn_custom:
showDialog(R.id.btn_custom);
break;
case R.id.btn_progress:
showDialog(R.id.btn_progress);
break;
case R.id.btn_single:
showDialog(R.id.btn_single);
break;
case R.id.btn_multi:
showDialog(R.id.btn_multi);
break;
}
}
@Override
public Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
switch (id) {
case R.id.btn_simple: {
builder.setTitle("my dialog btn_simple");
builder.setNegativeButton("取消", null);
builder.setPositiveButton("确定", null);
builder.setNeutralButton("中立", null);
break;
}
case R.id.btn_custom: {
builder.setTitle("my dialog btn_custom");
View view =LayoutInflater.from(this).inflate(R.layout.custom, null);
builder.setView(view);
builder.setPositiveButton("确定", null);
builder.setNegativeButton("取消", null);
builder.setNeutralButton("中立", null);
break;
}
case R.id.btn_progress: {
ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("正在下载");
return progressDialog;
}
case R.id.btn_single: {
builder.setTitle("my dialog btn_single");
builder.setSingleChoiceItems(new String[]{"aaa","bbb","ccc","ddd","eee"}, 0, null);
builder.setPositiveButton("确定", null);
builder.setNegativeButton("取消", null);
builder.setNeutralButton("中立", null);
break;
}
case R.id.btn_multi: {
builder.setTitle("my dialog btn_multi");
builder.setMultiChoiceItems(new String[]{"aaa","bbb","ccc","ddd","eee"},null, null);
builder.setPositiveButton("确定", null);
builder.setNegativeButton("取消", null);
builder.setNeutralButton("中立", null);
break;
}
}
return builder.create();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.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" >
<Button
android:id="@+id/btn_simple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="简单的Dialog" />
<Button
android:id="@+id/btn_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/btn_simple"
android:text="自定义Dialog" />
<Button
android:id="@+id/btn_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/btn_custom"
android:text="进度条Dialog" />
<Button
android:id="@+id/btn_single"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/btn_progress"
android:text="单选Dialog" />
<Button
android:id="@+id/btn_multi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/btn_single"
android:text="复选Dialog" />
</RelativeLayout>
custom.xml
<?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" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="300dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/img1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请在下面输入内容" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请在这里输入内容" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="以下是单选框" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aaa" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="bbb" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ccc" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ddd" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="eee" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="以下是复选框" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aaa" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="bbb" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ccc" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ddd" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="eee" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="fff" />
</LinearLayout>
</ScrollView>
</LinearLayout>