AlertDialog(彈跳視窗)


在做一個AlertDialog常用到的語法

setTitle:為對畫框設定標題
setIcon:為對畫框設定圖片
setMessage:設定對畫框的內容
setView:自行定義對畫框的樣式
setItems:為對畫框設定一個多選項的對話筐(類似Spinner)
setMultiChoiceItem:在對畫框中設定一系列的復選匡(類似CheckBox)
setNeutraButton:設定一個按鈕
setPositiveButton:在對畫框中設定一個“ok”的按鈕
setNegativeButton:在對畫框中設定一個“no”的按鈕
create:建立對畫框
show:顯示對畫框

建立一個簡單的Dialog

import android.app.Dialog;
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.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.drawable.ic_launcher)
                        .setTitle("一個Dialog")
                        .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).setNegativeButton("cancel",null).create()
                        .show();
            }
        });

    }
}


大量訊息的Dialog

public class MainActivity extends AppCompatActivity {
    Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.drawable.ic_launcher)
                        .setTitle("大量訊息")
                        .setMessage("這裡有很多訊息這裡有很多訊息這裡有很多訊息這裡有很多訊息這裡有很多訊息這裡有" +
                                "很多訊息這裡有很多訊息這裡有很多訊息這裡有很多訊息這裡有很多訊息這裡有很多訊息這" +
                                "裡有很多訊息這裡有很多訊息這裡有很多訊息這裡有很多訊息這裡有很多訊息這裡有很多訊息")
                        .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setNegativeButton("cancel",null)
                        .setNeutralButton("第三按鈕",null).create()
                        .show();
            }
        });

    }
}


單一選項的Dialog

首先要在Values建立一個新的XML(這邊我叫Arrays)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="dialog_arrays">
        <item>第一個</item>
        <item>第二個</item>
        <item>第三個</item>
        <item>第四個</item>
        <item>第五個</item>

    </string-array>

</resources>

再來在Main裡面寫下代碼

public class MainActivity extends AppCompatActivity {
    Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.drawable.ic_launcher)
                        .setTitle("單項選擇Spinner")
                        .setItems(R.array.dialog_arrays, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String[] Item=getResources().getStringArray(
                                        R.array.dialog_arrays
                                );
                                Toast.makeText(MainActivity.this,""+which+","+
                                        Item[which],Toast.LENGTH_LONG).show();
                            }
                        }).create().show();

            }
        });

    }
}


ProgressDialog

public class MainActivity extends AppCompatActivity {
    Button btn;
    int mprogress;
    ProgressDialog pDialog;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                pDialog = new ProgressDialog(MainActivity.this);
                pDialog.setIcon(R.drawable.ic_launcher);
                pDialog.setTitle("進度條Dialog");
                pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pDialog.setMax(100);
                pDialog.setButton(DialogInterface.BUTTON_POSITIVE, "左键",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                // TODO Auto-generated method stub
                            }
                        });
                pDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "右键",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {


                            }
                        });
                pDialog.setProgress(0);
                pDialog.show();
                mprogress = 0;

                handler.sendEmptyMessage(0);
            }
        });
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (mprogress >= 100) {
                    pDialog.dismiss();
                } else {
                    mprogress++;

                    pDialog.incrementProgressBy(1);

                    handler.sendEmptyMessageDelayed(0, 100);
                }
            }
        };
    }
}


建立一個Layout

<?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">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ok"
        android:id="@+id/button2" />
</LinearLayout>

在來在main裡面寫下

public class MainActivity extends AppCompatActivity {
    Button btn;
    int mprogress;
    ProgressDialog pDialog;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater=LayoutInflater.from(MainActivity.this);
                final View popView= inflater.inflate(R.layout.items,null);
                final AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setView(popView);
                final AlertDialog dialog=builder.create();
                dialog.show();

            }
        });
        }
}

就完成了


results matching ""

    No results matching ""