Я пытаюсь загрузить HTML-файл в AlertDialog, сделать ссылки доступными для кликов и правильно отформатировать диалог с заполнением.
В следующем коде показано диалоговое окно, но ссылка недоступна для щелчка.
public void showNews(int number) {
int last = shared.getInt("news",1);
Log.d("News", String.valueOf(last));
if (last >= number)
return;
AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this, R.style.darkAlertDialog); //, R.style.darkAlertDialog
builder.setTitle("Application News");
StringBuilder text = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("html/" + "news.html")));
String mLine;
while ((mLine = reader.readLine()) != null) {
text.append(mLine);
text.append('\n');
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
Log.e("showNews", "Error reading file");
return;
}
}
}
// Trouble
Spanned result;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
result = Html.fromHtml(text.toString(), Html.FROM_HTML_MODE_LEGACY);
} else {
result = Html.fromHtml(text.toString());
}
builder.setMessage(result);
// result.setMovementMethod(LinkMovementMethod.getInstance());
// result.setClickable(true);
// Trouble
builder.setPositiveButton( "Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// prefs.putInt("news", number);
// prefs.apply();
dialog.dismiss();
}
});
builder.create();
builder.show();
}
Установка перемещения и установка кликабельности приводит к синтаксической ошибке
Если я заменю раздел между // Проблема, как предложено в разделе «Использование HTML в диалоговом окне оповещений Android» на
TextView msg = new TextView(this);
msg.setText(Html.fromHtml(String.valueOf(text)));
builder.setView(msg);
msg.setMovementMethod(LinkMovementMethod.getInstance());
msg.setClickable(true);
ссылка активна, но между текстом и диалоговым окном нет отступа.
[![Пример из альтернативного кода][1]][1]
Я также пробовал использовать собственный макет, но он оказался пустым, хотя файлы журналов показывают, что текст есть.
Log.d("text", String.valueOf(text));
AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this, R.style.darkAlertDialog); //, R.style.darkAlertDialog
builder.setTitle("Application News");
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.app_news, null);
final TextView appNews = dialogView.findViewById(R.id.textNews);
appNews.setText(result);
appNews.setMovementMethod(LinkMovementMethod.getInstance());
appNews.setClickable(true);
Log.d("msg", String.valueOf(appNews.getText()));
builder.setView(dialogView);
[![enter image description here][2]][2]
[1]: https://i.sstatic.net/CbKUDjFr.jpg
[2]: https://i.sstatic.net/ZLdStP1m.jpg
Подробнее здесь: https://stackoverflow.com/questions/798 ... -clickable