Android对话框

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
//控件类型
private Button button1;
private AlertDialog.Builder builder;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Dialog dialog;
private View button8;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
//给控件添加监听
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
dialog();

}

@Override
public void onClick(View v) {
//switch判断用户的操作,实现功能
switch (v.getId()) {
case R.id.button1:
builder.create().show();
break;
case R.id.button2:
dialogg();
dialog.show();
break;
case R.id.button3:
new AlertDialog.Builder(this).setTitle("请输入").setIcon(
android.R.drawable.ic_dialog_info).setView(
new EditText(this)).setPositiveButton("确定", null)
.setNegativeButton("取消", null).show();
break;
case R.id.button4:
new AlertDialog.Builder(this).setTitle("复选框").setMultiChoiceItems(
new String[] { "Item1", "Item2" }, null, null)
.setPositiveButton("确定", null)
.setNegativeButton("取消", null).show();
break;
case R.id.button5:
new AlertDialog.Builder(this).setTitle("单选框").setIcon(
android.R.drawable.ic_dialog_info).setSingleChoiceItems(
new String[] { "Item1", "Item2" }, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).setNegativeButton("取消", null).show();
break;
case R.id.button6:
new AlertDialog.Builder(this).setTitle("列表框").setItems(
new String[] { "Item1", "Item2" }, null).setNegativeButton(
"确定", null).show();
break;
case R.id.button7:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.demo,
(ViewGroup) findViewById(R.id.dialog));

new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout)
.setPositiveButton("确定", null)
.setNegativeButton("取消", null).show();
break;
case R.id.button8:
ProgressDialog dialog2 = new ProgressDialog(this);
dialog2.setTitle("进度对话框");
dialog2.setMessage("我就是进度对话框");
dialog2.show();
break;
default:
break;
}
}

private void dialog() {
builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("提示");
builder.setIcon(R.drawable.ic_launcher);
builder.setMessage("确认删除吗?");
builder.setPositiveButton("确认", null);
builder.setNegativeButton("取消", null);
}

private void dialogg() {
dialog = new AlertDialog.Builder(this)
.setIcon(android.R.drawable.btn_star)
.setTitle("喜好调查")
.setMessage("你喜欢李连杰的电影吗?")
.setPositiveButton("很喜欢",
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "我很喜欢他的电影。",
Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("不喜欢",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "我不喜欢他的电影。",
Toast.LENGTH_LONG).show();
}
})
.setNeutralButton("一般", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "谈不上喜欢不喜欢。",
Toast.LENGTH_LONG).show();
}
}).create();
}
}

 

 

 

xml代码:

 

 

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/zong"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下三" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="复选框" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选框" />

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="表单" />

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义" />

<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进度" />
</LinearLayout>

</HorizontalScrollView>

posted @ 2017-10-11 19:27  芳草玫瑰下  阅读(171)  评论(0编辑  收藏  举报