我已經搜索並搜索了這個內容,但不幸的是我沒有找到任何關於如何在自定義對話框上重複使用以前充氣的視圖的有用信息。防止每次在自定義對話框上充氣
這是我試過(他們沒有工作)
方法1:
保持我alertdialog建設者全球和內onCreate()
做什麼,我需要:
private AlertDialog.Builder dialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new AlertDialog.Builder(this);
detailView = getLayoutInflater().inflate(R.layout.detail, null);
TextView textview1 = (TextView)detailView.findViewById(R.id.lblName);
TextView textview2 = (TextView)detailView.findViewById(R.id.lblSubtitle);
textview1.setText("Test1");
textview2.setText("Test2");
dialog.setView(detailView);
dialog.create();
btnSearch = (ImageView)findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.show();
}
});
}
然後只在需要時顯示它:dialog.show();
沒有工作,LogCat輸出T:
Uncaught handler: thread main exiting due to uncaught exception
java.lang.IllegalStateException: The specified child already has a parent.
You must call removeView() on the child's parent first.
方法2:
在onCreate()
剛剛膨脹的佈局,並在單擊事件:
dialog = new AlertDialog.Builder(this);
TextView textview1 = (TextView)detailView.findViewById(R.id.lblName);
TextView textview2 = (TextView)detailView.findViewById(R.id.lblSubtitle);
textview1.setText("Test1");
textview2.setText("Test2");
dialog.setView(detailView);
dialog.create();
但它墜毀給予同樣的錯誤輸出。
當然,我已經閱讀了輸出結果,並試圖找到父母,所以我猜父母會是對話框本身,但它沒有任何方法來刪除視圖,所以我不知道是什麼去做。
還有沒有辦法重用textview引用?
(我知道它的編碼不,只是想獲得它的工作,我會得到它正確地重構。
您是否特別關注它是一個提醒對話框?因爲它看起來並不像你希望它是可交互的,在這種情況下,你可以繼續使用自定義的「Toast dialog = new Toast(Activity)」來實現你想要的結果 – gaara87
它根本不會是交互式的,它只會顯示信息,但吐司將不夠用,因爲我將顯示覆雜的控件(圖像視圖,列表)。 – Areks
也使用inflate()中的第二個參數可能也有幫助 – gaara87