gpt4 book ai didi

android - 如何以编程方式使用默认的 AlertDialog 添加 TextView 和 EditText

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:25 25 4
gpt4 key购买 nike

我一直在尝试在默认的 AlertDialog 中添加两个元素,但我似乎无法让它工作。这是我的代码:

// START Dialog
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

TextView tv = new TextView(this);
tv.setText(title);
tv.setPadding(40, 40, 40, 40);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(20);

EditText et = new EditText(this);
etStr = et.getText().toString();

alertDialogBuilder.setView(et);
alertDialogBuilder.setTitle(title);
alertDialogBuilder.setMessage("Input Student ID");
alertDialogBuilder.setCustomTitle(tv);

if (isError)
alertDialogBuilder.setIcon(R.drawable.icon_warning);
// alertDialogBuilder.setMessage(message);
alertDialogBuilder.setCancelable(false);

// Setting Negative "Cancel" Button
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});

// Setting Positive "Yes" Button
alertDialogBuilder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (isError)
finish();
else {
Intent intent = new Intent(
ChangeDeviceActivity.this,
MyPageActivity.class);
startActivity(intent);
}
}
});

AlertDialog alertDialog = alertDialogBuilder.create();

try {
alertDialog.show();
} catch (Exception e) {
// WindowManager$BadTokenException will be caught and the app would
// not display the 'Force Close' message
e.printStackTrace();
}

现在,这只是一个 EditText,带有一条由 alertDialogBu​​ilder.setMessage("Input Student ID"); 设置的消息,但我想把它变成一个 TextView 这样我就可以将它居中对齐。我该怎么做?

最佳答案

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(parms);

layout.setGravity(Gravity.CLIP_VERTICAL);
layout.setPadding(2, 2, 2, 2);

TextView tv = new TextView(this);
tv.setText("Text View title");
tv.setPadding(40, 40, 40, 40);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(20);

EditText et = new EditText(this);
etStr = et.getText().toString();
TextView tv1 = new TextView(this);
tv1.setText("Input Student ID");

LinearLayout.LayoutParams tv1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
tv1Params.bottomMargin = 5;
layout.addView(tv1,tv1Params);
layout.addView(et, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

alertDialogBuilder.setView(layout);
alertDialogBuilder.setTitle(title);
// alertDialogBuilder.setMessage("Input Student ID");
alertDialogBuilder.setCustomTitle(tv);

if (isError)
alertDialogBuilder.setIcon(R.drawable.icon_warning);
// alertDialogBuilder.setMessage(message);
alertDialogBuilder.setCancelable(false);

// Setting Negative "Cancel" Button
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});

// Setting Positive "OK" Button
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (isError)
finish();
else {
Intent intent = new Intent(ChangeDeviceActivity.this,
MyPageActivity.class); startActivity(intent);
}
}
});

AlertDialog alertDialog = alertDialogBuilder.create();

try {
alertDialog.show();
} catch (Exception e) {
// WindowManager$BadTokenException will be caught and the app would
// not display the 'Force Close' message
e.printStackTrace();
}

关于android - 如何以编程方式使用默认的 AlertDialog 添加 TextView 和 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18565482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com