android alertdialog setview
AlertDialog.Builder builder = new AlertDialog.Builder(HomeFragment.this.getContext());
builder.setTitle("");
builder.setMessage("请输入相关信息:");
LinearLayout containerLayout = new LinearLayout(HomeFragment.this.getContext());
containerLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
containerLayout.setOrientation(LinearLayout.VERTICAL);
containerLayout.setPadding(40, 40, 40, 40); // 设置上下左右间距,单位为像素
EditText inputEditText = new EditText(HomeFragment.this.getContext());
inputEditText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
inputEditText.setHint("请输入内容");
TextView labelTextView = new TextView(HomeFragment.this.getContext());
labelTextView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
labelTextView.setText("标签框示例");
containerLayout.addView(inputEditText);
containerLayout.addView(labelTextView);
builder.setView(containerLayout);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String inputText = inputEditText.getText().toString();
dialog.cancel();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
标签:android
加入收藏