如何在 Android 中更改警报对话框的文本行
Posted
技术标签:
【中文标题】如何在 Android 中更改警报对话框的文本行【英文标题】:How to change the textline of an alertdialog in Android 【发布时间】:2020-09-13 08:57:56 【问题描述】:有谁知道你如何用 textinput 更改文本输入行和 AlertDialog 的光标(从绿色变为粉红色)。 1]:https://i.stack.imgur.com/1apfI.png 我在我的课堂上这样实现它:
AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.AlertDialogTheme);
builder.setTitle("Artikel Toevoegen");
final EditText input = new EditText(this);
input.setSingleLine();
builder.setView(input);
builder.setPositiveButton("Toevoegen", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
if(input.length() != 0)
shoppingList.add((input.getText().toString()));
lv.setAdapter(adapter);
else
Toast toast = Toast.makeText(getApplicationContext(),"Voer een artikel in", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 570);
toast.show();
artikelToevoegen();
);
builder.setNegativeButton("Annuleer", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
dialog.cancel();
);
AlertDialog dialog = builder.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = 40; //x position
wmlp.y = 100; //y position
dialog.show();
这就是 xml 样式的样子:
<style name="AlertDialogTheme" parent="ThemeOverlay.AppCompat.Dialog.Alert">
android:textCursorDrawable="@drawable/pink_cursor"
android:backgroundTint="#D70F64"
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#d70f64</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#d70f64</item>
</style>
【问题讨论】:
【参考方案1】:你可以改变颜色,
input.getBackground().setColorFilter(getResources().getColor(R.color.pink_cursor), PorterDuff.Mode.SRC_ATOP);
或
input.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.pink_cursor)));
【讨论】:
以上是关于如何在 Android 中更改警报对话框的文本行的主要内容,如果未能解决你的问题,请参考以下文章
根据javascript中的文本行数更改textarea的高度[重复]