Android 如何获取AlertDialog标题?
我试图获得信息,以下代码行起作用:Android 如何获取AlertDialog标题?,android,testing,dialog,Android,Testing,Dialog,我试图获得信息,以下代码行起作用: TextView dialogMessage = (TextView)dialogObject.findViewById(android.R.id.message); 但是,当我尝试使用以下行获取标题时,它返回null TextView dialogTitle = (TextView)dialogObject.findViewById(android.R.id.tittle); 我检查了警报对话框的代码。在内部,他们使用R.id.alertTitle初始化
TextView dialogMessage = (TextView)dialogObject.findViewById(android.R.id.message);
但是,当我尝试使用以下行获取标题时,它返回null
TextView dialogTitle = (TextView)dialogObject.findViewById(android.R.id.tittle);
我检查了
警报对话框
的代码。在内部,他们使用R.id.alertTitle
初始化AlertDialog
title的TextView
。您可以使用getIdentifier
检索它:
int titleId = getResources().getIdentifier( "alertTitle", "id", "android" );
if (titleId > 0) {
TextView dialogTitle = (TextView) dialogObject.findViewById(titleId);
if (dialogTitle != null) {
}
}
编辑:对于
AppCompat
,getIdentifier
的第三个参数应该是应用程序的包名。您可以使用context.getPackageName()
检索后者。我知道这是一篇旧文章,但我使用了公认的答案,并添加了一些对我有用的内容。可以使用下面的代码使对话框标题多行(默认为1行)。我还读取了预设标题,因为我后来使用ContentLoader异步添加了txt
int titleId = getResources().getIdentifier( "alertTitle", "id", "android" );
if (titleId > 0) {
TextView dialogTitle = (TextView) getDialog().findViewById(titleId);
if (dialogTitle != null) {
dialogTitle.setMaxLines(4);
dialogTitle.setSingleLine(false);
String txt = dialogTitle.getText().toString();
String txt1 = txt + ":\n\n" + "next line txt";
dialogTitle.setText(txt1);
}
}
我知道这个问题提到了AlertDialog,但如果您通过谷歌搜索来到这里,并寻找
DialogFragment
的答案:
getDialog().findViewById(android.R.id.title))
你可以自己实现它。创建自己的类,扩展android.app.AlertDialog.Builder。然后使用方法setTitle()创建一个变量来存储值
然后使用您可以将其用作普通的AlertDialog,并使用实现的方法获取其标题。然后您可以测试它:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Crete new alert dialog
AlertDialog dialog = new AlertDialog(this);
dialog.setMessage("Type some message here :D");
dialog.setTitle(R.string.settings_title); // string resource
dialog.setTitle("Dialog1 title"); // string
dialog.show();
// Crete secondary dialog with same title
// using getTitle() method
new AlertDialog(this)
.setTitle(dialog.getTitle())
.setMessage("Copy title from first dialog.")
.show();
}
}
为了避免代码被破坏,当谷歌决定在将来更改其对话框标题视图id时,这里有一个更可靠的解决方案 我们只需返回遇到的第一个
视图
,它的类型是TextView
//
// Usage: findFirstEncounteredType(getDialog().getWindow().getDecorView(), TextView.class)
//
public static View findFirstEncounteredType(View view, Class<? extends View> klass) {
if (klass.isInstance(view)) {
return view;
} else {
if (!(view instanceof ViewGroup)) {
return null;
}
}
ViewGroup viewGroup = (ViewGroup)view;
for (int i=0, ei=viewGroup.getChildCount(); i<ei; i++) {
View child = viewGroup.getChildAt(i);
View result = findFirstEncounteredType(child, klass);
if (result != null) {
return result;
}
}
return null;
}
//
//用法:FindFirstEncounterType(getDialog().getWindow().getDecorView(),TextView.class)
//
公共静态视图FindFirstEndCounteredType(查看视图,类id是alertTitle
,我不确定它在外部是否可见。尝试使用android.R.id.alertTitle
没有id alertTitle尽管人们说不可能,但这是一个很好的答案。你应该得到黑带,非常感谢。对于AppCompat AlertDialog,你应该使用getResources().getIdentifier(“alertTitle”、“id”、“您的.package.name”)@Blackbelt答案应该用Sergei Belozerov注释扩展。@doctorram答案是从2015年开始的。这实际上取决于您导入的警报对话框。当时没有类似于支持库中的警报对话框的奇特警报对话框。@Blackbelt奇特与否,现在的解决方案是什么?此解决方案不再有效。应该是一流的根本找不到文本视图!
//
// Usage: findFirstEncounteredType(getDialog().getWindow().getDecorView(), TextView.class)
//
public static View findFirstEncounteredType(View view, Class<? extends View> klass) {
if (klass.isInstance(view)) {
return view;
} else {
if (!(view instanceof ViewGroup)) {
return null;
}
}
ViewGroup viewGroup = (ViewGroup)view;
for (int i=0, ei=viewGroup.getChildCount(); i<ei; i++) {
View child = viewGroup.getChildAt(i);
View result = findFirstEncounteredType(child, klass);
if (result != null) {
return result;
}
}
return null;
}