我需要在一个Android程序弹出一个对话框,该对话框包含TextView/EditText/Button 等多个控件,借鉴《Create custom dialog using AlertDialog.Builder, with dynamic content 》这篇博文(可由google通过输入博文标题找到这篇博文),使用了如下的代码。技术关键点:1)Layout也是View;2)View需要指定Layout来使其正常工作。
- // 在一个Activity中,创建包含TextView 和EditText 的AlertDialog
- String dialogText = "登录完成: " + str;
- TextView textOutput = new TextView(this);
- textOutput.setText("调试日志:" + logStr + "\n将调试日志发送到以下手机号码");
- LayoutParams textOutputLayoutParams = new LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- textOutput.setLayoutParams(textOutputLayoutParams);
- EditText phoneNum = new EditText(this);
- phoneNum.setText("138****1111");
- phoneNum.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT));
- LinearLayout dialogLayout = new LinearLayout(this);
- dialogLayout.setOrientation(LinearLayout.VERTICAL);
- dialogLayout.addView(textOutput);
- dialogLayout.addView(phoneNum);
- dialogBuilder.setTitle(dialogText)
- .setIcon(android.R.drawable.ic_dialog_info)
- .setView(dialogLayout)
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Uri uri = Uri.parse("smsto:138****1111");//FIXME Hardcode
- Intent itnt = new Intent(Intent.ACTION_SENDTO, uri);
- itnt.putExtra("sms_body", logStr);
- startActivity(itnt);
- }
- })
- .setNegativeButton("取消", null)
- .show();
- }
完毕 by lucoz