安卓基礎(十七)

來源:互聯網
上載者:User

標籤:anrdoid   dialog   對話方塊   

對話方塊

  • 對話方塊
    • 簡介
    • 本文
    • 擴充閱讀

目標人群:沒有基礎的安卓初學者
知識點:DialogFragment的建立與調用
目標:在頁面中顯示一個含有兩個按鈕的對話方塊

簡介
  • 對話方塊的介紹已匯入準備

  • DialogFragment類的簡單調用

本文

1.對話方塊是出現在使用者眼前的一個小小的視窗,用於讓使用者即時的做出一些操作。在3.0之後的安卓版本中,統一建議使用DialogFragment來實現,對於低版本的安卓系統來說,首先我們需要在build.gradle中添加對support V4包的引用,代碼如下:

dependencies {    ...    compile ‘com.android.support:support-v4:21.0.3‘    ...}
  • 也可以在項目中選擇Open Module Settings-Dependencies-點擊右側加號-Library Dependency-選中appcompat-V4來進行添加

  • 如果為高於3.0之後的安卓版本,則不需要考慮此處

  • 以下樣本為support-v4版本

2.建立一個FragmentActivity頁面,命名為MainActivity,建立兩個方法來對應對話方塊按鈕的點擊事件:

    public void doPositiveClick() {        Log.i("FragmentAlertDialog", "Positive click!");    }    public void doNegativeClick() {        Log.i("FragmentAlertDialog", "Negative click!");    }

3.建立一個DialogFragment頁面,命名為MyAlertDialogFragment,並實現它的onCreateDialog方法,代碼如下:

import android.app.AlertDialog;import android.app.Dialog;import android.content.DialogInterface;import android.os.Bundle;import android.support.v4.app.DialogFragment;public class MyAlertDialogFragment extends DialogFragment {    public static MyAlertDialogFragment newInstance(String title) {        //建立一個新的對話方塊,並得到從調用頁面傳來的Title值        MyAlertDialogFragment frag = new MyAlertDialogFragment();        Bundle args = new Bundle();        args.putString("title", title);        frag.setArguments(args);        return frag;    }    @Override    public Dialog onCreateDialog(Bundle savedInstanceState) {        /** 得到Title值,並建立一個對話方塊,該對話方塊包含兩個按鈕           當兩個按鈕被點擊時,分別調用了MainActivity頁面的doPositiveClick和doNegativeClick方法 **/        String title = getArguments().getString("title");        return new AlertDialog.Builder(getActivity())                .setIcon(R.drawable.ic_launcher)                .setTitle(title)                .setPositiveButton("確定",                        new DialogInterface.OnClickListener() {                            public void onClick(DialogInterface dialog, int whichButton) {                                ((MainActivity) getActivity()).doPositiveClick();                            }                        }                )                .setNegativeButton("取消",                        new DialogInterface.OnClickListener() {                            public void onClick(DialogInterface dialog, int whichButton) {                                ((MainActivity) getActivity()).doNegativeClick();                            }                        }                )                .create();    }}

4.回到MainActivity中,使用代碼來調用該DialogFragment,代碼如下:

   @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        showDialog();    }    public void showDialog() {        DialogFragment newFragment = MyAlertDialogFragment.newInstance("兩個按鈕的對話方塊");        newFragment.show(getSupportFragmentManager(), "alert");    }
  • 當然也可以將該Fragment作為視圖結構中的一員,以此來調用該DialogFragment
FragmentTransaction ft = getFragmentManager().beginTransaction();DialogFragment newFragment = MyDialogFragment.newInstance();ft.add(R.id.embedded, newFragment);ft.commit();
擴充閱讀
  1. DialogFragment的官方API
  2. Dialogs的官方API

安卓基礎(十七)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.