Android:在AlertDialog框中为编辑文本设置边距

时间:2016-04-19 06:36:41

标签: android android-edittext alertdialog android-alertdialog android-5.1.1-lollipop

我正在尝试创建警告对话框,例如 Lollipop ,一切都很顺利,但我在一个部分中遇到 EditText

我希望带有下划线的 EditText 和带有20dp的左右边距。对于下划线,我尝试了 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"));

是否有任何方法可以为 EditText 设置后退,其功能在API 16下方, setMargin 为左侧和右侧设置为的EditText

3 个答案:

答案 0 :(得分:5)

根视图上的边距不起作用。尝试在其他答案中添加填充到父布局。
但是,不是在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进行更新的Dialog设计。

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>

答案 1 :(得分:2)

使用此代码对我有用。

     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);

答案 2 :(得分:0)

这对您有帮助

这对我有用...

通过将def residual(variables,X,y): a_0 = variables[0] a = variables[1] return (y - a_0 * np.exp(X.dot(a)))**2 X = np.random.randn(100,5) y = np.random.randint(low=0,high=2,size=100) a_0 = 1 a = np.random.randn(X.shape[1]) leastsq(residual,[a_0,a],args=(X,y)) 设置为RelativeLayout的根,因为根视图上的边距无效。

EditText
相关问题