Android_(控件)使用AlertDialog實現點擊Button顯示出多選框


 

 

單擊"更多"按鈕,顯示出多選框

 

運行截圖:

 

 

 程序結構

(本想通過Button中android:background使用drawable資源下的圖片作為按鈕背景,設計太丑就去掉了Σ(= = !))

 

package com.example.asus.a7gary01;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.更多);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                builder.setItems(getResources().getStringArray(R.array.Gary), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
            }
        });
    }
}
MainActivity

 

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.asus.a7gary01.MainActivity">


    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/添加"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="添加" />

        <Button
            android:id="@+id/保存"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="保存" />

        <Button
            android:id="@+id/發送"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="發送" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/詳細"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="詳細" />

        <Button
            android:id="@+id/查找"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="查找" />

        <Button
            android:id="@+id/更多"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="更多" />
    </TableRow>

</TableLayout>
activity_main.xml

 

<resources>
    <string name="app_name">7Gary01</string>
    <array name="Gary">
        <item>幫助</item>
        <item>分享</item>
        <item>刪除</item>
        <item>撥號</item>
    </array>
</resources>
strings.xml

 

一、界面布局

 

使用表格布局,放置六個按鈕

 

 

二、功能實現

 

添加對Button的點擊事件響應

 button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 創建構建器
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                //在點擊偵聽器上建立新的對話框接口
                //getResources()獲得資源
                builder.setItems(getResources().getStringArray(R.array.Gary), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                //顯示多選框
                builder.show();
            }
        });

 

AlertDialog的六種創建方式:傳送門

 

創建AlertDialog的步驟:

  1、創建AlertDialog.Builder對象

  2、調用Builder對象的setTitle方法設置標題,setIcon方法設置圖標

  3、調用Builder相關方法如setMessage方法、setItems方法、setSingleChoiceItems方法、setMultiChoiceItems方法、setAdapter方法、setView方法設置不同類型的對話框內容。

  4、調用setPositiveButton、setNegativeButton、setNeutralButton設置多個按鈕

  5、調用Builder對象的create()方法創建AlertDialog對象

  6、調用AlertDialog對象的show()方法將對話框顯示出來

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM