日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 基类fragment,Android DialogFragment 基类的定制

發布時間:2024/9/27 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 基类fragment,Android DialogFragment 基类的定制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鴻洋博客介紹:DialogFragment的基本使用

好處:

1:使用DialogFragment來管理對話框,當旋轉屏幕和按下后退鍵時可以更好的管理其聲明周期,它和Fragment有著基本一致的生命周期

2.DialogFragment也允許開發者把Dialog作為內嵌的組件進行重用,類似Fragment(可以在大屏幕和小屏幕顯示出不同的效果)

使用方法:

Dialoger.build(getActivity())

.setContentText("你好防盜鏈發交電費")

.setContentTitle("標題")

.setNegativeBtn("重新認證", new Dialoger.OnNegativeListener() {

@Override public void onNegative(Dialog dialog) {

dialog.dismiss();

....

....

} })

.setPositiveBtn("確認", new Dialoger.OnPositiveListener() {

@Override public void onPositive(Dialog dialog) {

dialog.dismiss();

....//對應的操作

....

}

})

.dialoger.setOnCancelListener(new DialogInterface.OnCancelListener() {

@Override

public void onCancel(DialogInterface dialog) {

view.getViewActivity().finish();

}

}). show();

封裝:既然DialogFragment比普通的dialog好這么多,豈有不封裝哪來用的道理

使用DialogFragment至少需要實現onCreateView或者onCreateDIalog方法。onCreateView即使用定義的xml布局文件展示Dialog。onCreateDialog即利用AlertDialog或者Dialog創建出Dialog。下面的封裝采用onCreateView的方式來封裝

1.構建dialog 設置基本參數

public static Dialoger build(FragmentActivity activity, String transactionTag, Bundle args) {

Dialoger dialoger = new Dialoger();

dialoger.setActivity(activity);//設置傳入的activity對象

dialoger.setTransactionTag(transactionTag);//設置的tag標志(和Fragment一樣,便于利用tag獲取當前的Activity對象,來傳遞數據)

dialoger.setArguments(args);//設置的bundle參數

dialoger.setCancelable(false);//初始化設置為不可點擊

dialoger.setTheme(0);//設置沒有主題

dialoger.setContentView(R.layout.dialog_default);//設置默認的dialog的View

return dialoger;

}

public static Dialoger build(FragmentActivity activity) {

return build(activity, Dialoger.class.getSimpleName(), null);

}

2.外界傳入對應的參數 (標題,context 內容 定義的View)

private void setActivity(FragmentActivity mActivity) {

this.mActivity = mActivity;

}

private void setTransactionTag(String transactionTag) {

this.mTransactionTag = transactionTag;

}

public Dialoger setContentView(int layoutResID) {

this.mContentView = LayoutInflater.from(mActivity).inflate(layoutResID, null);

return this;

}

public Dialoger setContentView(View view) {

this.mContentView = view;

return this;

}

public Dialoger setTheme(int theme) {

setStyle(DialogFragment.STYLE_NO_TITLE, theme);

return this;

}

public Dialoger setContentTitle(String title) {

mContentTitle = title;

return this;

}

public Dialoger setContentText(String text) {

mContentText = text;

return this;

}

public Dialoger setContentTitle(String title, int titleColor) {

mContentTitle = title;

mContentTitleColor = titleColor;

return this;

}

public Dialoger setContentText(String text, int textColor) {

mContentText = text;

mContentTextColor = textColor;

return this;

}

3.對傳入進來的參數進行配置

@Nullable

@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//設置透明背景

return mContentView;

}

@Override

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

if (mCancelListener != null) {//只有當設置了取消監聽的時候,才可以設置取消

setCancelable(true);

}

//設置標題

TextView mTitleView = (TextView) view.findViewById(R.id.dialog_title_tv);

if (!TextUtils.isEmpty(mContentTitle)) {

mTitleView.setText(mContentTitle);

if (mContentTitleColor != 0) {

mTitleView.setTextColor(mContentTitleColor);

}

} else {

mTitleView.setVisibility(View.GONE);

}

//設置內容

TextView mTextView = (TextView) view.findViewById(R.id.dialog_text_tv);

if (!TextUtils.isEmpty(mContentText)) {

mTextView.setText(mContentText);

if (mContentTextColor != 0) {

mTextView.setTextColor(mContentTextColor);

}

} else {

mTextView.setVisibility(View.GONE);

}

//設置 確定 和 取消 按鈕

View buttonPanel = view.findViewById(R.id.buttons_layout);

buttonPanel.setVisibility(View.GONE);

boolean negativeEnable = false;

boolean positiveEnable = false;

//如果有確定按鈕 設置 ----->可點擊 內容 監聽

Button negativeButton = (Button) view.findViewById(R.id.btn_negative);

if (TextUtils.isEmpty(mNegativeText)) {

negativeButton.setVisibility(View.GONE);

} else {

negativeEnable = true;

buttonPanel.setVisibility(View.VISIBLE);

negativeButton.setVisibility(View.VISIBLE);

negativeButton.setText(mNegativeText);

negativeButton.setOnClickListener(this);

if (mNegativeTextColor != 0) {

negativeButton.setTextColor(mNegativeTextColor);

}

}

//如果有取消按鈕 設置 ----->可點擊 內容 監聽

Button positiveButton = (Button) view.findViewById(R.id.btn_positive);

if (TextUtils.isEmpty(mPositiveText)) {

positiveButton.setVisibility(View.GONE);

} else {

positiveEnable = true;

buttonPanel.setVisibility(View.VISIBLE);

positiveButton.setVisibility(View.VISIBLE);

positiveButton.setText(mPositiveText);

positiveButton.setOnClickListener(this);

if (mPositiveTextColor != 0) {

positiveButton.setTextColor(mPositiveTextColor);

}

}

View contentDivider = view.findViewById(R.id.horizontal_divider);

View buttonDivider = view.findViewById(R.id.vertical_divider);

//設置布局的分割線 顯示和隱藏

if (positiveEnable) {

if (negativeEnable) {

contentDivider.setVisibility(View.VISIBLE);

buttonDivider.setVisibility(View.VISIBLE);

} else {

contentDivider.setVisibility(View.GONE);

buttonDivider.setVisibility(View.GONE);

}

buttonPanel.setVisibility(View.VISIBLE);

} else {

if (negativeEnable) {

contentDivider.setVisibility(View.VISIBLE);

buttonPanel.setVisibility(View.VISIBLE);

buttonDivider.setVisibility(View.GONE);

} else {

contentDivider.setVisibility(View.GONE);

}

}

}

4.設置確定 取消按鈕 和 取消監聽

/**

* 確認

*

* @param text

* @param listener

* @return

*/

public Dialoger setPositiveBtn(String text, OnPositiveListener listener) {

mPositiveText = text;

mPositiveListener = listener;

return this;

}

public Dialoger setPositiveColor(int textColor) {

mPositiveTextColor = textColor;

return this;

}

/**

* 取消按鈕(不是點擊dialog其他地方的取消)

*

* @param text

* @param listener

* @return

*/

public Dialoger setNegativeBtn(String text, OnNegativeListener listener) {

mNegativeText = text;

mNegativeListener = listener;

return this;

}

public Dialoger setNegativeColor(int textColor) {

mNegativeTextColor = textColor;

return this;

}

//取消監聽

public void setOnCancelListener(@Nullable DialogInterface.OnCancelListener listener) {

if (listener != null) {

mCancelListener = listener;

}

}

//調用dialog的取消

@Override

public void onCancel(DialogInterface dialog) {

super.onCancel(dialog);

if (mCancelListener != null) {

mCancelListener.onCancel(dialog);

}

}

5.傳遞 確認 取消 的點擊事件來回調 第4條的監聽

@Override

public void onClick(View v) {

if (v.getId() == R.id.btn_negative) {

if (mNegativeListener != null) mNegativeListener.onNegative(getDialog());

} else if (v.getId() == R.id.btn_positive) {

if (mPositiveListener != null) mPositiveListener.onPositive(getDialog());

}

}

6.xml布局

style="@style/DT_DIALOG_THEME"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:layout_margin="30dip"

android:background="@drawable/dialog_bg_color"

android:minWidth="270dp"

android:orientation="vertical">

android:id="@+id/dialog_title_tv"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:layout_marginTop="20dp"

android:gravity="center_horizontal"

android:text="提示"

android:textColor="#333333"

android:textSize="18sp"/>

android:id="@+id/dialog_text_tv"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="20dp"

android:layout_marginTop="30dp"

android:gravity="center"

android:paddingLeft="20dp"

android:paddingRight="20dp"

android:text="提示"

android:textColor="#686871"

android:textSize="16sp"/>

android:id="@+id/horizontal_divider"

style="@style/horizontal_divider"/>

android:id="@+id/buttons_layout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/btn_negative"

android:layout_width="0dip"

android:layout_height="44dip"

android:layout_weight="1"

android:background="@android:color/transparent"

android:gravity="center"

android:paddingTop="2dip"

android:text="取消"

android:textColor="#8e8e9c"

android:textSize="17sp"/>

android:id="@+id/vertical_divider"

style="@style/vertical_divider"/>

android:id="@+id/btn_positive"

android:layout_width="0dip"

android:layout_height="44dip"

android:layout_weight="1"

android:background="@android:color/transparent"

android:gravity="center"

android:paddingTop="2dip"

android:text="確定"

android:textColor="#f73e3e"

android:textSize="16sp"/>

7.xml布局簡單效果

Paste_Image.png

再來回顧下使用方法:采用的建造者模式

Dialoger.build(getActivity())

.setContentText("你好防盜鏈發交電費")

.setContentTitle("標題")

.setNegativeBtn("重新認證", new Dialoger.OnNegativeListener() {

@Override public void onNegative(Dialog dialog) {

dialog.dismiss();

....

....

} })

.setPositiveBtn("確認", new Dialoger.OnPositiveListener() {

@Override public void onPositive(Dialog dialog) {

dialog.dismiss();

....//對應的操作

....

}

})

.dialoger.setOnCancelListener(new DialogInterface.OnCancelListener() {

@Override

public void onCancel(DialogInterface dialog) {

view.getViewActivity().finish();

}

}). show();

over~

總結

以上是生活随笔為你收集整理的android 基类fragment,Android DialogFragment 基类的定制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。