gpt4 book ai didi

java - 更改显示在 alertBox 中的 EditText(任何 View )的大小?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:13:59 24 4
gpt4 key购买 nike

我想为用户输入显示一个 alertBox。所以我在 alertBox 中添加了 EditText View 。我的 alertBox 代码是:

final EditText input = new EditText(this);
//input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
input.setX(10);
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
builder.setMessage("Title")
.setCancelable(false)
.setView(input)
.setTitle("Create new Playlist")
.setPositiveButton("CREATE",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// finish the current activity
// AlertBoxAdvance.this.finish();
playlistName = input.getText().toString();
dialog.cancel();


}
})
.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel the dialog box
dialog.cancel();
}
});
android.app.AlertDialog alert = builder.create();
alert.show();

这会给出以下警告对话框:

enter image description here

但 EditText 不是从标题和警报消息开始的地方开始的(缩进不正确)。所以我想要以下类型的 EditText,它在警报对话框中有适当的缩进。

enter image description here

如何做到这一点?如何改变警报对话中的观点立场?或者如何改变 View 的大小?

最佳答案

试试这个:

XML 布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">

<EditText
android:id="@+id/editTextField"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />

</LinearLayout>

Java 代码:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mActivity);
LayoutInflater inflater = mActivity.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog, null);
dialogBuilder.setView(dialogView);

final EditText mEditText = (EditText) dialogView.findViewById(R.id.editTextField);

dialogBuilder.setTitle("Title");
dialogBuilder.setMessage("Type your message here");

dialogBuilder.setPositiveButton("Yes", null);
dialogBuilder.setNegativeButton("No", null);

final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

@Override
public void onShow(DialogInterface dialog) {
Button positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
String enteredTextString= mEditText.getText().toString();
//To whatever with the text entered
}
});
Button negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
negativeButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
});
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {

}
});
alertDialog.show();

关于java - 更改显示在 alertBox 中的 EditText(任何 View )的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40480637/

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