Android: 设置警告对话框中编辑文本的边距
Android: Set Margin for Edit Text in AlertDialog Box
我正在尝试创建一个 Alert Dialog 框,如 Lollipop,一切正常,但我被困在一个部分共 EditText
我想要 EditText 带下划线和 左右边距 带 20dp.For 下划线 我试过 setBackground() ,它工作正常。
但是有一个问题是setBackground()不会生效API 16级以下.
我试过setMargin
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(30,0,30,0);
input.setLayoutParams(lp);
input.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
input.setBackground(getResources().getDrawable(R.drawable.edit_text_line));
builder.setView(input);
但编辑文本时使用全父宽度。
完整代码
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Message");
builder.setMessage("Do you want to\n"+""+"exit from app");
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(30,0,30,0);
input.setLayoutParams(lp);
input.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
input.setBackground(getResources().getDrawable(R.drawable.edit_text_line)); //call reequires api 16 and above
builder.setView(input);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You exit from app " + input.getText().toString(),
Toast.LENGTH_LONG).show();
}
});
AlertDialog alert = builder.create();
alert.show();
Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
nbutton.setTextColor(Color.parseColor("#7e7e7e"));
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
pbutton.setTextColor(Color.parseColor("#109c8f"));
有什么方法可以为低于 API 16 和 [=27= 的 EditText 设置 background ]setMargin左右为EditText.
使用此代码对我有用。
final EditText input = new EditText(MainActivity.this);
input.setSingleLine();
FrameLayout container = new FrameLayout(thisActivity);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.topMargin = convertDpToPx(30);
params.bottomMargin = convertDpToPx(30);
input.setLayoutParams(params);
container.addView(input);
根视图的边距不起作用。尝试按照其他答案中的说明向父布局添加填充。
但与其在 java 中创建对话框布局,我建议您膨胀 xml 并使用 AppCompatEditText 如果您想使用线条背景
这是给你的示例代码
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Message");
// Why are you setting message here when you are inflating custom view?
// You need to add another TextView in xml if you want to set message here
// Otherwise the message will not be shown
// builder.setMessage("Do you want to\n"+""+"exit from app");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null);
final AppCompatEditText input = (AppCompatEditText) view.findViewById(R.id.editText);
builder.setView(view);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You exit from app " + input.getText().toString(),
Toast.LENGTH_LONG).show();
}
});
AlertDialog alert = builder.create();
alert.show();
最后,您无法在创建对话框后立即获得按钮。如果您想自定义按钮文本颜色,您需要在 OnShowListener 中执行此操作。或者使用 android.support.v7.app.AlertDialog
用于较新的对话框设计。
Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
// will be null
// nbutton.setTextColor(Color.parseColor("#7e7e7e"));
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
// will be null
// pbutton.setTextColor(Color.parseColor("#109c8f"));
dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="16dp"
app:backgroundTint="@color/colorPrimary"/>
</LinearLayout>
希望对您有帮助
这对我有用...
通过将 RelativeLayout
设置为 EditText
的根,因为 根视图上的边距将不起作用。
public void ShowDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setTitle("App Name");
alertDialog.setMessage("Message");
final EditText input = new EditText(getContext());
input.setHint("Hint Text");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(36,36,36,36);
input.setLayoutParams(lp);
RelativeLayout container = new RelativeLayout(getContext());
RelativeLayout.LayoutParams rlParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
container.setLayoutParams(rlParams);
container.addView(input);
//now set view to dialog
alertDialog.setView(container);
alertDialog.setPositiveButton("Ohk", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (input.getText().toString().isEmpty()) {
input.setText("No Valid");
return;
}
}
});
alertDialog.show();
}
我正在尝试创建一个 Alert Dialog 框,如 Lollipop,一切正常,但我被困在一个部分共 EditText
我想要 EditText 带下划线和 左右边距 带 20dp.For 下划线 我试过 setBackground() ,它工作正常。
但是有一个问题是setBackground()不会生效API 16级以下.
我试过setMargin
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(30,0,30,0);
input.setLayoutParams(lp);
input.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
input.setBackground(getResources().getDrawable(R.drawable.edit_text_line));
builder.setView(input);
但编辑文本时使用全父宽度。
完整代码
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Message");
builder.setMessage("Do you want to\n"+""+"exit from app");
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(30,0,30,0);
input.setLayoutParams(lp);
input.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
input.setBackground(getResources().getDrawable(R.drawable.edit_text_line)); //call reequires api 16 and above
builder.setView(input);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You exit from app " + input.getText().toString(),
Toast.LENGTH_LONG).show();
}
});
AlertDialog alert = builder.create();
alert.show();
Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
nbutton.setTextColor(Color.parseColor("#7e7e7e"));
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
pbutton.setTextColor(Color.parseColor("#109c8f"));
有什么方法可以为低于 API 16 和 [=27= 的 EditText 设置 background ]setMargin左右为EditText.
使用此代码对我有用。
final EditText input = new EditText(MainActivity.this);
input.setSingleLine();
FrameLayout container = new FrameLayout(thisActivity);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.topMargin = convertDpToPx(30);
params.bottomMargin = convertDpToPx(30);
input.setLayoutParams(params);
container.addView(input);
根视图的边距不起作用。尝试按照其他答案中的说明向父布局添加填充。
但与其在 java 中创建对话框布局,我建议您膨胀 xml 并使用 AppCompatEditText 如果您想使用线条背景
这是给你的示例代码
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Message");
// Why are you setting message here when you are inflating custom view?
// You need to add another TextView in xml if you want to set message here
// Otherwise the message will not be shown
// builder.setMessage("Do you want to\n"+""+"exit from app");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null);
final AppCompatEditText input = (AppCompatEditText) view.findViewById(R.id.editText);
builder.setView(view);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You exit from app " + input.getText().toString(),
Toast.LENGTH_LONG).show();
}
});
AlertDialog alert = builder.create();
alert.show();
最后,您无法在创建对话框后立即获得按钮。如果您想自定义按钮文本颜色,您需要在 OnShowListener 中执行此操作。或者使用 android.support.v7.app.AlertDialog
用于较新的对话框设计。
Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
// will be null
// nbutton.setTextColor(Color.parseColor("#7e7e7e"));
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
// will be null
// pbutton.setTextColor(Color.parseColor("#109c8f"));
dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="16dp"
app:backgroundTint="@color/colorPrimary"/>
</LinearLayout>
希望对您有帮助
这对我有用...
通过将 RelativeLayout
设置为 EditText
的根,因为 根视图上的边距将不起作用。
public void ShowDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setTitle("App Name");
alertDialog.setMessage("Message");
final EditText input = new EditText(getContext());
input.setHint("Hint Text");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(36,36,36,36);
input.setLayoutParams(lp);
RelativeLayout container = new RelativeLayout(getContext());
RelativeLayout.LayoutParams rlParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
container.setLayoutParams(rlParams);
container.addView(input);
//now set view to dialog
alertDialog.setView(container);
alertDialog.setPositiveButton("Ohk", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (input.getText().toString().isEmpty()) {
input.setText("No Valid");
return;
}
}
});
alertDialog.show();
}