日志

android alertdialog setview

发表于 2024-05-20 zhangxinwei
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
加入收藏

会员评论登录

评论


汉字公式

我的作品

其他媒体

关于汉字公式

回到首页

后台数据库查询耗时: 1407 微秒

@2015-2023 最精简的解释,来自于自然、来自于生活

豫ICP备2023033703号-1