text以编程方式创建的edittexts的输入布局

时间:2016-04-23 13:19:16

标签: android android-support-library android-textinputlayout

我已经在android activity.java文件中以编程方式创建了edittexts。我想为edittexts添加textinputlayout。有没有办法做到这一点。我已将edittext包装在xml文件中。我希望以编程方式为edittexts创建相同的内容。

提前谢谢。

(--helpers.nsh--)
!define CreateSymbolicLinkFolder "!insertmacro CreateSymbolicLinkFolder"
!ifdef CreateSymbolicLinkFolder_   ; <--- always false
    Function CreateSymbolicLinkFolderFunc
        Exch $0   ; _TARGET
        Exch 1
        Exch $1   ; _JUNCTION
        IfFileExists "$0" 0 done
        ${GetParent} "$1" $R0
        CreateDirectory "$R0"
        System::Call "kernel32::CreateSymbolicLinkW(w `$1`, w `$0`, i 1) i .s"
        done:
        Pop $R0
        Pop $1
        Pop $0
    FunctionEnd
!endif
!macro CreateSymbolicLinkFolder _JUNCTION _TARGET
    !define CreateSymbolicLinkFolder_   ; <--- not work...
    Push "${_JUNCTION}"
    Push "${_TARGET}"
    Call CreateSymbolicLinkFolderFunc
!macroend

(--sample.nsi--)
!include "helpers.nsh"
...
Function Clean
    ${CreateSymbolicLinkFolder} "$APPDATA\SOMEPATH" "$EXEDIR\Data"
FunctionEnd
Section "Main"
    Call Clean
SectionEnd

2 个答案:

答案 0 :(得分:1)

您可以像这样从XML文件中扩充布局:

在布局res中创建text_input_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.TextInputEditText
        android:inputType="phone"
        android:hint="change hint"
        android:id="@+id/text_input_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>

然后在你的代码中充气

TextInputLayout txtInputLayout = (TextInputLayout) LayoutInflater.from(getActivity()).inflate(R.layout.text_input_layout,null);
TextInputEditText txtInputEditText = (TextInputEditText) txtInputLayout.findViewById(R.id.text_input_edit_text);
mRoot.addView(txtInputLayout);

答案 1 :(得分:0)

尝试将TextInputLayout视为EditText字段的包装器,例如:

    LinearLayout assessmentLayout = (LinearLayout) findViewById(R.id.assessmentsLayout);
    LinearLayout.LayoutParams layoutParams =
            new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
    assert assessmentLayout != null;

    TextView screenTitle = new TextView(this);
    screenTitle.setLayoutParams(layoutParams);
    screenTitle.setText(String.format("Assessment: %s", course.getName()));
    assessmentLayout.addView(screenTitle);

    TextInputLayout titleWrapper = new TextInputLayout(this);
    titleWrapper.setLayoutParams(layoutParams);
    titleWrapper.setHint("Assessment Title:");
    assessmentLayout.addView(titleWrapper);

    title = new EditText(this);
    title.setLayoutParams(layoutParams);
    title.setText(assessment.getTitle());
    titleWrapper.addView(title);

    TextInputLayout descriptionWrapper = new TextInputLayout(this);
    descriptionWrapper.setLayoutParams(layoutParams);
    descriptionWrapper.setHint("Description:");
    assessmentLayout.addView(descriptionWrapper);

    description = new EditText(this);
    description.setLayoutParams(layoutParams);
    description.setText(assessment.getDescription());
    descriptionWrapper.addView(description);

将EditText视图添加到TextInputLayout包装器而不是直接添加到活动布局。

相关问题