国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發之基于DialogFragment創建對話框的方法示例

2019-12-12 02:13:58
字體:
來源:轉載
供稿:網友

本文實例講述了Android基于DialogFragment創建對話框的方法。分享給大家供大家參考,具體如下:

/** * 使用DialogFragment創建對話框 * @description: * @author ldm * @date 2016-5-12 下午2:00:01 */public class FragmentAlertDialog extends Activity {  private Button button;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.fragment_dialog);    // 初始化Button及設置監聽    button = (Button) findViewById(R.id.show);    button.setOnClickListener(new OnClickListener() {      public void onClick(View v) {        // 彈出對話框        showDialog();      }    });  }  void showDialog() {    // DialogFragment 創建對話框    DialogFragment newFragment = MyAlertDialogFragment        .newInstance(R.string.alert_dialog_two_buttons_title);    newFragment.show(getFragmentManager(), "dialog");  }  public void doPositiveClick() {    Log.i("FragmentAlertDialog", "Positive click!");  }  public void doNegativeClick() {    Log.i("FragmentAlertDialog", "Negative click!");  }  /**   * 自定義彈出對話框DialogFragmet   *   * @description:   * @author ldm   * @date 2016-5-12 下午1:54:31   */  public static class MyAlertDialogFragment extends DialogFragment {    public static MyAlertDialogFragment newInstance(int title) {      MyAlertDialogFragment frag = new MyAlertDialogFragment();      Bundle args = new Bundle();      args.putInt("title", title);      frag.setArguments(args);      return frag;    }    /**     * DialogFragment需要實現onCreateView或者onCreateDIalog方法。     * onCreateView():使用定義的xml布局文件展示Dialog。     * onCreateDialog():利用AlertDialog或者Dialog創建出Dialog。     */    @Override    public Dialog onCreateDialog(Bundle savedInstanceState) {      int title = getArguments().getInt("title");      return new AlertDialog.Builder(getActivity())//創建一個Dialog     .setIcon(R.drawable.alert_dialog_icon)//設置圖標     .setTitle(title)//設置標題     .setPositiveButton(R.string.alert_dialog_ok,       new DialogInterface.OnClickListener() {//確認(OK)按鈕        public void onClick(DialogInterface dialog,          int whichButton) {         ((FragmentAlertDialog) getActivity())           .doPositiveClick();        }       })     .setNegativeButton(R.string.alert_dialog_cancel,//取消(Cancel)按鈕       new DialogInterface.OnClickListener() {        public void onClick(DialogInterface dialog,          int whichButton) {         ((FragmentAlertDialog) getActivity())           .doNegativeClick();        }       }).create();    }  }}

布局文件

<?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:gravity="center_horizontal"  android:orientation="vertical"  android:padding="4dip" >  <TextView    android:id="@+id/text"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center_vertical|center_horizontal"    android:layout_weight="1"    android:gravity="top|center_horizontal"    android:text="Example of displaying an alert dialog with a DialogFragment"    android:textAppearance="?android:attr/textAppearanceMedium" />  <Button    android:id="@+id/show"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_weight="0"    android:text="@string/show" >  </Button></LinearLayout>

開源代碼:https://github.com/ldm520/ANDROID_API_DEMOS

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青岛市| 招远市| 滦南县| 延吉市| 凉山| 都匀市| 昌平区| 剑阁县| 土默特左旗| 广河县| 寻甸| 尖扎县| 南江县| 蕲春县| 宝鸡市| 万荣县| 阳城县| 盐池县| 绥中县| 西充县| 嵊泗县| 桐城市| 西城区| 津市市| 阿鲁科尔沁旗| 平山县| 仙居县| 涿鹿县| 景宁| 灵寿县| 射洪县| 沾益县| 茶陵县| 吴堡县| 沂源县| 花莲市| 凭祥市| 罗山县| 凯里市| 黄梅县| 潞城市|