Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 對話框AlertDialog的基本創建,對話框alertdialog

對話框AlertDialog的基本創建,對話框alertdialog

編輯:關於android開發

對話框AlertDialog的基本創建,對話框alertdialog


 

測試代碼:

 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.zzw.testalerdialog.MainActivity" >

    <Button
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="點擊" />

</RelativeLayout>

 

JAVA代碼:

package com.zzw.testalerdialog;

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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Activity context = this;
        findViewById(R.id.but).setOnClickListener(new OnClickListener() {

            // DialogInterface.BUTTON_POSITIVE只要作用是顯示順序
            @Override
            public void onClick(View v) {
                /*
                //橫向顯示
                AlertDialog dialog = new AlertDialog.Builder(context).create();
                dialog.setIcon(R.drawable.ic_launcher);
                dialog.setTitle("標題");
                dialog.setMessage("提示信息");
                dialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "確定", 0).show();
                    }
                });

                dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "取消", 0).show();
                    }
                });

                dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "考慮", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "考慮", 0).show();
                    }
                });

                dialog.show();
                */
                
                //豎向顯示
                final String[] items = new String[3];
                for (int i = 0; i < 3; i++) {
                    items[i] = "選項--" + i;
                }

                AlertDialog dialog = new AlertDialog.Builder(context)
                        .setItems(items, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Toast.makeText(context, items[which], Toast.LENGTH_SHORT).show();
                    }
                }).create();

                dialog.setTitle("標題");
                dialog.setIcon(R.drawable.ic_launcher);

                dialog.show();
            }
        });
    }

}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved