Android:无法为Title设置TextView

时间:2015-07-27 18:48:53

标签: java android textview

我正在使用之前StackOverflow个问题中提出的技术,建议使用TextViews并将其中的属性设置为setTitleAlertDialog的参数。我需要这个,所以我可以设置标题的font

问题是Android Studio说它

cannot resolve method android.widget.TextView

以下是我的代码:

  TextView settingsTitle =  new TextView(this);
    settingsTitle.setText("Settings");
    settingsTitle.setTypeface(Typeface.createFromFile("monospace"));

    new AlertDialog.Builder(this)
            .setTitle(settingsTitle)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // continue with delete
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do nothing
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();

1 个答案:

答案 0 :(得分:1)

将您的代码替换为

new AlertDialog.Builder(this)
            .setTitle(settingsTitle.getText().toString())
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // continue with delete
                }
            })

因为没有名为setTitle()的方法,其参数类型为TextView

因此使用settingsTitle.getText().toString()获取TextView中的字符串并使用它来设置标题

setTitle(settingsTitle.getText().toString())

docs

中详细了解相关信息
相关问题