當前位置:編程學習大全網 - 源碼下載 - 如何通過反射來解決AlertDialog標題由於字數過多顯示不全的問題

如何通過反射來解決AlertDialog標題由於字數過多顯示不全的問題

首先入口處應該是這裏:

[java] view plain copy

builder.setTitle("關於印發《省環境監察局關於開展黨的群眾路線教育實踐活動的實施方案》的通知");

然後進入setTitle的方法:

[java] view plain copy

/**

* Set the title displayed in the {@link Dialog}.

*

* @return This Builder object to allow for chaining of calls to set methods

*/

public Builder setTitle(CharSequence title) {

P.mTitle = title;

return this;

}

好了,它把字符串賦給了對象P,然後再來看看P的類型:

[java] view plain copy

public static class Builder {

private final AlertController.AlertParams P;

private int mTheme;

/**

* Constructor using a context for this builder and the {@link AlertDialog} it creates.

*/

public Builder(Context context) {

this(context, resolveDialogTheme(context, 0));

}

嗯,從Builder處可以看到P是類型為AlertController.AlertParams的對象。然後再接著看AlertController.AlertParams這個類裏面的屬性(註意:如果妳沒有專門設置過可以查看Android內部類的方法的話,這裏是看不了的,相關設置可以參見:/kf/201311/259006.html):

好了,進入AlertController.AlertParams類內可以看到該類是屬於AlertController的內部類,以下為該類的部分屬性:

[java] view plain copy

public static class AlertParams {

public final Context mContext;

public final LayoutInflater mInflater;

public int mIconId = 0;

public Drawable mIcon;

public int mIconAttrId = 0;

public CharSequence mTitle;

好了,所以那個字符串設置時最終會設置到這個類對象的mTitle處,然後接下來就是要查看這個屬性什麽時候被使用了呢:

在內類可以看到該方法使用了該屬性:

[java] view plain copy

public void apply(AlertController dialog) {

if (mCustomTitleView != null) {

dialog.setCustomTitle(mCustomTitleView);

} else {

if (mTitle != null) {

dialog.setTitle(mTitle);

}

這裏說明mTitle被設置給了AlertController的對象dialog,然後接下來就是尋找這個dialog對象是怎麽被傳入進來的:

通過尋找找到apply該方法的被調用處是:

[java] view plain copy

/**

* Creates a {@link AlertDialog} with the arguments supplied to this builder. It does not

* {@link Dialog#show()} the dialog. This allows the user to do any extra processing

* before displaying the dialog. Use {@link #show()} if you don't have any other processing

* to do and want this to be created and displayed.

*/

public AlertDialog create() {

final AlertDialog dialog = new AlertDialog(P.mContext, mTheme, false);

P.apply(dialog.mAlert);

dialog.setCancelable(P.mCancelable);

if (P.mCancelable) {

dialog.setCanceledOnTouchOutside(true);

}

dialog.setOnCancelListener(P.mOnCancelListener);

dialog.setOnDismissListener(P.mOnDismissListener);

if (P.mOnKeyListener != null) {

dialog.setOnKeyListener(P.mOnKeyListener);

}

return dialog;

}

該方法位於AlertDialog.Builder的內部,也就是當Builder對象調用create方法時會將AlertController的對象dialog傳入,這裏可以看到是dialog的mAlert屬性,也就是說dialog的mAlert屬性是AlertController的對象。好了,到這裏分析完畢。現在就剩下取出該對象,對該對象進行反射了。

[java] view plain copy

AlertDialog dialog = builder.create();

try {

Class<?> mAlert = dialog.getClass();

Log.e("sahadev", mAlert.getName());

Field field = mAlert.getDeclaredField("mAlert");

field.setAccessible(true);

Log.e("sahadev", field.getName() + "----" + field.get(dialog));

Field mTitleView = field.get(dialog).getClass().getDeclaredField("mTitleView");

mTitleView.setAccessible(true);

Object AlertController = field.get(dialog);

mTitleView.set(AlertController, new TextView(this));//該方法<span style="font-family:Microsoft YaHei;">沒起作用,不知道為什麽,有大神清楚麽?</span>

dialog.show();

Object obj = mTitleView.get(AlertController);

TextView textView = (TextView) obj;

textView.setSingleLine(false);

} catch (Exception e) {

e.printStackTrace();

}

好了,到了這裏就解決完畢了。

  • 上一篇:java中彈出式菜單怎麽用show方法顯示 import java.awt.*; import javax.swing.*; public class S{ public s
  • 下一篇:自己怎麽做網址開網站
  • copyright 2024編程學習大全網