規定對話方塊

weixin_30639719發表於2020-04-05

 先寫佈局,來四個按鈕

<?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" >
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:id="@+id/btn1"
        android:text="點選顯示"
        />
     <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:id="@+id/btn2"
        android:text="單選顯示"
        />
     <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:id="@+id/btn3"
        android:text="多選顯示"
        />
     <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:id="@+id/btn4"
        android:text="列表"
        />


</LinearLayout>

然後主檔案

package com.example.deemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    String[] single_list = {"man","woman","女博士","程式設計師"};
    String[] multi_list = {"小鳥","靜流","千早","露西亞","朱音","篝"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initEvent();
    }
    private void initEvent(){//點選事件
        findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {//確定對話方塊
            
            @Override
            public void onClick(View arg0) {
                showDialog1();
                
            }
        });
        findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {//單選對話方塊
            
            @Override
            public void onClick(View arg0) {
                showDialog2();
                
            }
        });
        findViewById(R.id.btn3).setOnClickListener(new OnClickListener() {//多選對話方塊
            
            @Override
            public void onClick(View arg0) {
                showDialog3();
                
            }
        });
        findViewById(R.id.btn4).setOnClickListener(new OnClickListener() {//列表對話方塊
            
            @Override
            public void onClick(View arg0) {
                showDialog4();
                
            }
        });
    }
    
    private void showDialog1(){//顯示確認對話方塊
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("確認dialog");
        builder.setIcon(R.drawable.ic_launcher);//圖示
        builder.setMessage("提示內容");
        builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {//確定按鈕

            @Override
            public void onClick(DialogInterface dialog, int arg1) {
                Toast.makeText(MainActivity.this, "點選確定", Toast.LENGTH_SHORT).show();
                
            }
            });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {//取消按鈕
            
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(MainActivity.this, "點選取消", Toast.LENGTH_SHORT).show();
                
            }
        });
        AlertDialog dialog = builder.create();//獲取dialog
        dialog.show();//顯示dialog
    }
    
    private void showDialog2(){//單選對話方塊
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("選擇Dialog");
        builder.setIcon(R.drawable.ic_launcher);//圖示
        builder.setSingleChoiceItems(single_list, 0, new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface arg0, int arg1) {//第二個為傳入第幾個選擇
                String str = single_list[arg1];
                Toast.makeText(MainActivity.this, "this is a"+str, Toast.LENGTH_SHORT).show();
            }
        });//單選按鈕描述,預設選中
        
        AlertDialog dialog = builder.create();//獲取dialog
        dialog.show();//顯示dialog
    }
    private void showDialog3(){//多選對話方塊
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("選擇Dialog");
        builder.setIcon(R.drawable.ic_launcher);//圖示
        builder.setMultiChoiceItems(multi_list, null, new DialogInterface.OnMultiChoiceClickListener() {
            
            @Override
            public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
                if(arg2){
                    Toast.makeText(MainActivity.this, "我的老婆們是"+multi_list[arg1], Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MainActivity.this, "還不是我的老婆們是"+multi_list[arg1], Toast.LENGTH_SHORT).show();
                }
                
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {//取消按鈕,其實是隱藏
            
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                arg0.dismiss();
            }
        });
        AlertDialog dialog = builder.create();//獲取dialog
        dialog.show();//顯示dialog
    }
    private void showDialog4(){//單選對話方塊
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("列表");
        builder.setIcon(R.drawable.ic_launcher);//圖示
        builder.setItems(multi_list, new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(MainActivity.this, "我老婆"+multi_list[arg1], Toast.LENGTH_SHORT).show();
                
            }
        });
        //什麼取消啊確定啊什麼功能根據上面自己一加
        AlertDialog dialog = builder.create();//獲取dialog
        dialog.show();//顯示dialog
    }
}
    

還有一個自定義佈局

首先寫一個自定義佈局檔案,名字比如dialog_layout.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" >
    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <EditText 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="內容"
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:layout_marginLeft="10dp"
        />
    </LinearLayout>
    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/four"
        />
</LinearLayout>

然後java檔案的一部分

點選事件

findViewById(R.id.btn5).setOnClickListener(new OnClickListener() {//列表對話方塊
            
            @Override
            public void onClick(View arg0) {
                showDialog5();
                
            }
        });

然後對話方塊

private void showDialog5(){//自定義對話方塊
        LayoutInflater inflater = LayoutInflater.from(this);//獲取佈局
        View view  = inflater.inflate(R.layout.dialog_layout, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("自定義Dialog");
        builder.setIcon(R.drawable.ic_launcher);//圖示
        builder.setView(view);//傳入view
        
        AlertDialog dialog = builder.create();//獲取dialog
        dialog.show();//顯示dialog
    }

 

轉載於:https://www.cnblogs.com/webgavin/p/5746346.html

相關文章