1. 程式人生 > >Android對話框

Android對話框

布局 line layout etc lns ati settitle 輸入 undle

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>

Android對話框