1. 程式人生 > >android 幾種對話方塊的簡單實現

android 幾種對話方塊的簡單實現

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="請輸入"/>
<Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="確定1"/> </LinearLayout>

MainActivity.java

import android.content.DialogInterface;
import
android.support.v7.app.ActionBar; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity
extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 普通對話方塊 * * @param view */ public void show1(View view) { final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle("對話方塊"); builder.setMessage("內容"); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "確定", Toast.LENGTH_LONG).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_LONG).show(); } }); builder.show(); } /** * 單選對話方塊 * * @param view */ public void show2(View view) { final String single_item[] = {"男", "女", "大一", "大二"}; final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle("對話方塊"); builder.setSingleChoiceItems(single_item, 1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "" + single_item[which], Toast.LENGTH_LONG).show(); } }); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "退出", Toast.LENGTH_LONG).show(); dialog.dismiss(); } }); builder.create().show(); } /** * 多選對話方塊 * * @param view */ public void show3(View view) { final String single_item[] = {"男", "女", "大一", "大二"}; final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle("對話方塊"); builder.setMultiChoiceItems(single_item, null , new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { Toast.makeText(MainActivity.this, "" + single_item[which], Toast.LENGTH_LONG).show(); } } }); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "退出", Toast.LENGTH_LONG).show(); dialog.dismiss(); } }); builder.create().show(); } /** * 自定義對話方塊 * * @param view */ public void show4(View view) { final String single_item[] = {"男", "女", "大一", "大二"}; final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle("對話方塊"); //把佈局載入view View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null); builder.setView(view1); Button button = (Button) view1.findViewById(R.id.bt1); final AlertDialog dialog = builder.create(); dialog.show(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "退出", Toast.LENGTH_LONG).show(); dialog.dismiss(); } }); } }

執行結果就不給了,自己執行下就行了

本人剛剛學安卓,有錯誤不要噴