Android:以编程方式添加EditText - 内容关闭屏幕

时间:2017-11-07 01:09:37

标签: android android-layout

我有一个标签和一个输入字段(const theRest = sliced.map((item, i) => { item[i].name; }) ),我以编程方式添加到EditText。当我输入比输入字段长的文本时,值将偏离屏幕。我应该如何解决这个问题,以便我可以看到我正在键入的内容,还可以回滚到输入字段的开头?

键入TableLayout时的示例:

enter image description here

布局XML:

A-Z1-9

代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.DocumentActivity">

<TableLayout
    android:id="@+id/document_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
</TableLayout>

2 个答案:

答案 0 :(得分:0)

嗨,你必须删除这一行

inputField.maxLines = 1

并使用LayoutParams

LayoutParams params = new LayoutParams(
        LayoutParams.WRAP_CONTENT,      
        LayoutParams.WRAP_CONTENT
);
inputField.setLayoutParams(params);

希望它会对你有所帮助:)。

答案 1 :(得分:0)

我已设法使用垂直LinearLayout组件作为主占位符来实现此功能,然后使用LinearLayoutViewText组件填充水平EditText组件。< / p>

我生成15个'标签'和'输入字段'的完整实现是:

布局XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.DocumentActivity"
tools:layout_editor_absoluteY="81dp"
android:id="@+id/activity_document">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/document_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>

<强>代码:

    var documentLayout: LinearLayout = findViewById(R.id.document_layout)

    var lp = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
    lp.leftMargin = 8
    lp.rightMargin = 8

    for (i in 0..15) {
        var linearLayout = LinearLayout(this)
        linearLayout.id = View.generateViewId()
        linearLayout.layoutParams = lp

        val inputField = EditText(this)
        inputField.id = View.generateViewId()
        inputField.setSingleLine()
        inputField.layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT, 1f)

        val label = TextView(this)
        label.id = View.generateViewId()
        label.text = "Label $i"
        label.layoutParams = LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT, 0f)

        linearLayout.addView(label)
        linearLayout.addView(inputField)
        documentLayout.addView(linearLayout)
    }
相关问题