AlertDialog對話視窗(關於)  

 


java:
 
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

import android.view.Menu;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     
        final Button button01 = (Button) findViewById(R.id.button1);
        final Button button02 = (Button) findViewById(R.id.button2);      
        button01.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0) {
             About();
        }
        });      
        button02.setOnClickListener(new OnClickListener(){
         public void onClick(View arg0) {
             Leave();
        }
        });
    }
    private void About() {
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("關於")
              .setMessage("這是 Alert Dialog")
              .show();
     }
    private void Leave() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("確定要離開本程式嗎?")
          .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int which) {
              // TODO Auto-generated method stub
        MainActivity.this.finish();
           }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                 // TODO Auto-generated method stub
                dialog.cancel();
               }
           });
          AlertDialog about_dialog = builder.create();
           about_dialog.show();
  }    
}

 


xml:

<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=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="關於" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:text="離開本程式?" />

</RelativeLayout>

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 小Chiu 的頭像
    小Chiu

    Cuiu's Home

    小Chiu 發表在 痞客邦 留言(0) 人氣()