public class PopUpDialog extends Dialog { Context context; private View customView; public PopUpDialog(Context context) { super(context); this.context = context; // TODO Auto-generated constructor stub } public PopUpDialog(Context context, int theme){ super(context, theme); this.context = context; LayoutInflater inflater= LayoutInflater.from(context); customView = inflater.inflate(R.layout.mydialog, null); } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(customView); } @Override public View findViewById(int id) { // TODO Auto-generated method stub return super.findViewById(id); } public View getCustomView() { return customView; } }
customView = inflater.inflate(R.layout.mydialog, null);通过此语句获取view“指针”(借用C的术语),在新的Activity中实现调用自定义对话框中的控件。
PopUpDialog newDialog = new PopUpDialog(MsgReView.this, R.style.MyDialog); newDialog.setCanceledOnTouchOutside(true); View view = newDialog.getCustomView(); TextView text1 = (TextView)view.findViewById(R.id.textViewTotal); text1.setText("调查人数:5");