本文介紹了如何使用 setText() 在自定義布局對話框中編輯文本的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我在對話框的自定義布局中有一個 TextView.對話框即將出現時,必須更改其文本.
I have a TextView in a custom layout for dialog. Its text has to be changed when the dialog is about to appear.
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/final_score"
/>
我用來設置文本和顯示對話框的java代碼是
java code I used to set text and show dialog is
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.its_over, null));
AlertDialog dialog = builder.create();
dialog.show();
TextView t = (TextView)findViewById(R.id.final_score);
t.setText(""+score);
我也試過這個代碼.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.its_over, null));
AlertDialog dialog = builder.create();
TextView t = (TextView)dialog.findViewById(R.id.final_score);
t.setText(""+score);
dialog.show();
但調用這些方法時應用會崩潰.
but the app would crash when these method is called.
但如果我們刪除
TextView t = (TextView)dialog.findViewById(R.id.final_score);
t.setText(""+score);
它不會崩潰.
推薦答案
嘗試通過它的父引用訪問TextView
Try to access the TextView by it's parent referance
View view = inflater.inflate(R.layout.its_over, null);
builder.setView(view);
TextView t = (TextView) view.findViewById(R.id.final_score);
這篇關于如何使用 setText() 在自定義布局對話框中編輯文本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!