使用 [=e10=] 而不是键盘输入时 EditText 视图调整大小
EditText view resizeing when using "setText" instead of keyboard input
我有一个 table 布局,前两行有 2 个 EditText,其他行有一堆按钮,就像自定义键盘一样。 (为简单起见,仅包含下面 xml 中的第一行按钮)
使用 android:scrollbars="vertical"
将 EditText 视图设置为可滚动。当我按下自定义键盘上的按钮时,我将值添加到输入字符串,然后使用 EditText.setText()
方法设置文本。我的问题是,如果文本太长,它会转到下一行,同时调整整个 EditText 视图的大小。否则,用默认的 android 键盘做同样的事情就像一个魅力,在不调整大小的情况下继续下一行并在右侧显示滚动条。
这是XML
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dip">
<EditText
android:layout_weight="1"
android:scrollbars="vertical"
android:layout_height="match_parent"
android:id="@+id/input"
android:layout_span="2"
android:text="Type an expression to begin..." />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dip">
<EditText
android:layout_weight="1"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:id="@+id/output"
android:layout_span="2"
android:text="Real time calculation"/>
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF0000" />
<TableRow
android:id="@+id/tableRow3"
android:layout_weight="1"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp" >
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_clear"
android:text="C" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_divide"
android:text="/" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_multiply"
android:text="*" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_delete"
android:text="DEL" />
</TableRow>
</TableLayout>
setText()
方法有什么问题?
此外,我想在两个 EditText 上添加一个背景图像,调整它的大小以适应视图,而不需要调整 EditText 的大小。
如有任何建议,我们将不胜感激
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<EditText
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:background="@null"
android:id="@+id/input"
android:text="Type an expression to begin...\n\n\n\n\n\n\n\n\n ddfsfs\n\n\n erwr dsf" />
</ScrollView>
<View
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#220000" />
<ScrollView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<EditText
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:background="@null"
android:id="@+id/output"
android:text="Real time calculation"/>
</ScrollView>
<View
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#FF0000" />
<LinearLayout
android:id="@+id/tableRow3"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/ic_cross"
android:layout_weight="1"
android:padding="15dp"
android:layout_height="match_parent"
android:id="@+id/button_clear"
android:text="C" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/ic_cross"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_divide"
android:text="/" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/ic_cross"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_multiply"
android:text="*" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/ic_cross"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_delete"
android:text="DEL" />
</LinearLayout>
</LinearLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dip">
<EditText
android:layout_weight="1"
android:scrollbars="vertical"
android:layout_height="match_parent"
android:id="@+id/input"
android:layout_span="2"
android:maxLines="1"
android:lines="1"
android:inputType="text"
android:text="Type an expression to begin..." />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dip">
<EditText
android:layout_weight="1"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:id="@+id/output"
android:layout_span="2"
android:maxLines="1"
android:lines="1"
android:inputType="text"
android:text="Real time calculation"/>
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF0000" />
<TableRow
android:id="@+id/tableRow3"
android:layout_weight="1"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp" >
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_clear"
android:text="C" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_divide"
android:text="/" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_multiply"
android:text="*" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_delete"
android:text="DEL" />
</TableRow>
</TableLayout>
使用这个可以解决您的问题。
我有一个 table 布局,前两行有 2 个 EditText,其他行有一堆按钮,就像自定义键盘一样。 (为简单起见,仅包含下面 xml 中的第一行按钮)
使用 android:scrollbars="vertical"
将 EditText 视图设置为可滚动。当我按下自定义键盘上的按钮时,我将值添加到输入字符串,然后使用 EditText.setText()
方法设置文本。我的问题是,如果文本太长,它会转到下一行,同时调整整个 EditText 视图的大小。否则,用默认的 android 键盘做同样的事情就像一个魅力,在不调整大小的情况下继续下一行并在右侧显示滚动条。
这是XML
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dip">
<EditText
android:layout_weight="1"
android:scrollbars="vertical"
android:layout_height="match_parent"
android:id="@+id/input"
android:layout_span="2"
android:text="Type an expression to begin..." />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dip">
<EditText
android:layout_weight="1"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:id="@+id/output"
android:layout_span="2"
android:text="Real time calculation"/>
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF0000" />
<TableRow
android:id="@+id/tableRow3"
android:layout_weight="1"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp" >
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_clear"
android:text="C" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_divide"
android:text="/" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_multiply"
android:text="*" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_delete"
android:text="DEL" />
</TableRow>
</TableLayout>
setText()
方法有什么问题?
此外,我想在两个 EditText 上添加一个背景图像,调整它的大小以适应视图,而不需要调整 EditText 的大小。
如有任何建议,我们将不胜感激
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<EditText
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:background="@null"
android:id="@+id/input"
android:text="Type an expression to begin...\n\n\n\n\n\n\n\n\n ddfsfs\n\n\n erwr dsf" />
</ScrollView>
<View
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#220000" />
<ScrollView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<EditText
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:background="@null"
android:id="@+id/output"
android:text="Real time calculation"/>
</ScrollView>
<View
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#FF0000" />
<LinearLayout
android:id="@+id/tableRow3"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/ic_cross"
android:layout_weight="1"
android:padding="15dp"
android:layout_height="match_parent"
android:id="@+id/button_clear"
android:text="C" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/ic_cross"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_divide"
android:text="/" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/ic_cross"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_multiply"
android:text="*" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/ic_cross"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_delete"
android:text="DEL" />
</LinearLayout>
</LinearLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dip">
<EditText
android:layout_weight="1"
android:scrollbars="vertical"
android:layout_height="match_parent"
android:id="@+id/input"
android:layout_span="2"
android:maxLines="1"
android:lines="1"
android:inputType="text"
android:text="Type an expression to begin..." />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dip">
<EditText
android:layout_weight="1"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:id="@+id/output"
android:layout_span="2"
android:maxLines="1"
android:lines="1"
android:inputType="text"
android:text="Real time calculation"/>
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF0000" />
<TableRow
android:id="@+id/tableRow3"
android:layout_weight="1"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp" >
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_clear"
android:text="C" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_divide"
android:text="/" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_multiply"
android:text="*" />
<Button
android:layout_width="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:background="@drawable/button_custom_bg"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/button_delete"
android:text="DEL" />
</TableRow>
</TableLayout>
使用这个可以解决您的问题。