我在android應用上工作。我現在有一個應用程序有2 button
爲「用戶信息」和「收費」,當點擊每個按鈕必須顯示自己dialog
關聯。對話框的內容是之前設計的2個佈局,並且設置爲contentView
。我的問題是通過單擊每個按鈕只顯示dialog_user_info。而點擊btnCharge
時,實際上必須顯示layout_sharzh,點擊btnUserInfo
時顯示layout_user_info。如何解決?當點擊`btnCharge`時顯示layout_sharzh,點擊`btnUserInfo`時顯示layout_user_info(對話框)
這裏是我的代碼:
btnCharge.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(dialog_charge,null);
}
});
btnUserInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(dialog_user_info,null);
}
});
(另外我以後 public class MainActivity extends Activity
寫道final int dialog_charge=1 , dialog_user_info=2;
和private Dialog dialog=null;
,對活動的頂部),這是onCreateDialog
方法:
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case dialog_charge :
dialog=new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = LayoutInflater.from(this);
View dialogview = inflater.inflate(R.layout.layout_sharzh, null);
// now pass dialogview to setView of Dialog
dialog.setContentView(dialogview);
btnSave=(Button)dialogview.findViewById(R.id.btnSave);
btnReturn=(Button)dialogview.findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dialog.cancel();
}
});
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"success",Toast.LENGTH_SHORT).show();
}
});
case dialog_user_info:
dialog=new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater1 = LayoutInflater.from(this);
dialogview = inflater1.inflate(R.layout.layout_user_info, null);
// now pass dialogview to setView of Dialog
dialog.setContentView(dialogview);
dialog.setCanceledOnTouchOutside(true);
}
return dialog;
}
的可能重複[爲什麼我們需要case語句後打破?](http://stackoverflow.com/questions/2710300/why-這樣做,我們-需要,盈虧後的病例報告) – 2014-12-09 17:34:48