本文介紹了如何使用 setText() 在自定義布局對(duì)話框中編輯文本的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我在對(duì)話框的自定義布局中有一個(gè) TextView.對(duì)話框即將出現(xiàn)時(shí),必須更改其文本.
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"
/>
我用來(lái)設(shè)置文本和顯示對(duì)話框的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);
我也試過(guò)這個(gè)代碼.
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();
但調(diào)用這些方法時(shí)應(yīng)用會(huì)崩潰.
but the app would crash when these method is called.
但如果我們刪除
TextView t = (TextView)dialog.findViewById(R.id.final_score);
t.setText(""+score);
它不會(huì)崩潰.
推薦答案
嘗試通過(guò)它的父引用訪問(wèn)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);
這篇關(guān)于如何使用 setText() 在自定義布局對(duì)話框中編輯文本的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!