Skip to content

Commit

Permalink
利用反射来设置mdalert的标题和msg样式
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Mar 23, 2018
1 parent 728f176 commit 62bee51
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ public void onChoosen( List<Integer> selectedIndex, List<CharSequence> selectedS
public void onGetChoose(boolean[] states) {
super.onGetChoose(states);
}
}).show();
}).setTitleColor(R.color.dialogutil_ios_btntext_blue).show();
break;
case R.id.btn_singlechoose:
String[] words2 = new String[]{"12","78","45","89","88","00"};
Expand Down
37 changes: 27 additions & 10 deletions dialog/src/main/java/com/hss01248/dialog/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
Expand All @@ -39,6 +40,7 @@
import com.hss01248.dialog.material.MdInputHolder;
import com.hss01248.dialog.view.DialogUtil_DialogActivity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -96,6 +98,11 @@ public static void showDialog(final Dialog dialog, final ConfigBean bean) {
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
if (bean.alertDialog!= null){
setMdBtnStytle(bean);
setTitleMessageStyle(bean.alertDialog,bean);
//setListItemsStyle(bean);
}
bean.listener.onShow();
//showSoftKeyBoardDelayed(bean.needSoftKeyboard,bean.viewHolder);
//showSoftKeyBoardDelayed(bean.needSoftKeyboard,bean.customContentHolder);
Expand All @@ -107,11 +114,7 @@ public void onShow(DialogInterface dialog) {
public void run() {
try {
dialog.show();
if (bean.alertDialog!= null){
setMdBtnStytle(bean);
setTitleMessageStyle(bean.alertDialog,bean);
//setListItemsStyle(bean);
}

adjustWindow(dialog,bean);
}catch (Exception e){
e.printStackTrace();
Expand All @@ -128,25 +131,37 @@ public void run() {
}

private static void setTitleMessageStyle(final Dialog dialog,ConfigBean bean) {
TextView tvMessage = ((TextView)dialog.getWindow().getDecorView().findViewById(android.R.id.message));
TextView tvTitle = ((TextView)dialog.getWindow().getDecorView().findViewById(android.R.id.title));
if(tvMessage!=null){

try {
Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
mAlert.setAccessible(true);
Object mAlertController = mAlert.get(dialog);


Field mMessage = mAlertController.getClass().getDeclaredField("mMessageView");
mMessage.setAccessible(true);
TextView tvMessage = (TextView) mMessage.get(mAlertController);
if(bean.msgTxtColor !=0){
tvMessage.setTextColor(getColor(bean.context,bean.msgTxtColor));
}
if(bean.msgTxtSize !=0){
tvMessage.setTextSize(bean.msgTxtSize);
}
}

if(tvTitle!=null){
Field titleView = mAlertController.getClass().getDeclaredField("mTitleView");
titleView.setAccessible(true);
TextView tvTitle = (TextView) titleView.get(mAlertController);
if(bean.titleTxtColor !=0){
tvTitle.setTextColor(getColor(bean.context,bean.titleTxtColor));
}
if(bean.titleTxtSize !=0){
tvTitle.setTextSize(bean.titleTxtSize);
}

} catch (Exception e) {
e.printStackTrace();
}

}

private static void adjustWindow(final Dialog dialog, final ConfigBean bean) {
Expand All @@ -157,6 +172,8 @@ public void onGlobalLayout() {
setBottomSheetDialogPeekHeight(bean);
adjustWH(dialog,bean);



dialog.getWindow().getDecorView().getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
Expand Down
Loading

0 comments on commit 62bee51

Please sign in to comment.