2013年1月8日 星期二

對話框(Dialog)

對話框,故名其思,是一個讓應用程式與使用者「對話」的元件。應用程式透過對話盒與使用者進行下述的對話:
* 詢問問題:使用者回答 Yes/No
* 詢問偏好:使用者選擇自已偏好的項目,可以是單選,也可以是複選
* 說明狀態:讓使用者知道應用程式目前的狀態,例如:顯示「處理中」、「載入中」等訊息

AlertDialog物件

package com.rex.dialog;

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

public class Main extends Activity {
    private Button dialogButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        dialogButton = (Button) findViewById(R.id.dialogButton);
        final AlertDialog alertDialog= getAlertDialog("這是一個對話框","請選擇......");
        dialogButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View view) {
                 alertDialog.show();
                
            }
            
        });
    }

    private AlertDialog getAlertDialog(String title, String message) {
    //產生一個Builder物件 建立對話盒內容的產生器物件
    Builder builder = new AlertDialog.Builder(Main.this);
    //設定Dialog的標題
     builder.setTitle(title);
    //設定Dialog的內容
     builder.setMessage(message);
    //設定Positive按鈕資料
     builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
        
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //按下按鈕時顯示快顯
        Toast.makeText(Main.this, "您按下OK按鈕", Toast.LENGTH_SHORT).show();
            
        }
    });
    //設定Negative按鈕資料
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
         @Override
           public void onClick(DialogInterface dialog, int which) {
          //按下按鈕時顯示快顯
          Toast.makeText(Main.this, "您按下Cancel按鈕", Toast.LENGTH_SHORT).show();
           }
      });
      //利用Builder物件建立AlertDial
     
        return builder.create();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

main.xml(Layout)


<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"
    tools:context=".Main" >

    <Button
        android:id="@+id/dialogButton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="33dp"
        android:layout_marginTop="20dp"
        android:text="ShowDialog" />

</RelativeLayout>
     
    

----------------------------------------------------------------------------------------------------------


沒有留言:

張貼留言