如何在有焦点或在android中选择时将编辑文本带到前面

时间:2013-01-08 09:48:04

标签: android select focus android-edittext android-softkeyboard

我在屏幕上有3个编辑文本。在触摸edittext之前的小屏幕设备上,我的屏幕看起来像

enter image description here

触摸第一个edittext后(例如在名字前面),键盘弹出来了,它看起来

enter image description here

即。现在edittext Amount(在TextView前面名为Amount)现在位于用户面前。但是当我触摸(或点击)第一个(名称)编辑文本时,我希望它在用户面前。但无论每次金额在前面我选择哪一个。

我尝试过使用requestFocus()和setSelect(true)。即使焦点返回真实,但没有任何作用。

那怎么做?

我已经向tablelayout提供了滚动视图,其中包含这三个edittexts和前面的三个文本框。

我的xml代码是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/rl_upload_header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:minHeight="50dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/selected_image_detail"
        android:textSize="20sp"
        android:textStyle="bold" />
</LinearLayout>

<LinearLayout
    android:id="@+id/rl_upload_top"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_weight="4"
    android:baselineAligned="false"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:contentDescription="@string/empty" >
    </ImageView>
</LinearLayout>

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="2"
    android:gravity="center" >

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="5dip" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/name_table" />

            <EditText
                android:id="@+id/ed_img_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/text_box_background"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:hint="@string/name"
                android:paddingLeft="5dip" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="5dip" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/amount_table" />

            <EditText
                android:id="@+id/ed_amount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/text_box_background"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:hint="@string/amount"
                android:paddingLeft="5dip" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="5dip" >

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/desc_table" />

            <EditText
                android:id="@+id/ed_desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/text_box_background"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:hint="@string/desc"
                android:paddingLeft="5dip" />
        </TableRow>
    </TableLayout>
</ScrollView>

<LinearLayout
    android:id="@+id/rl_upload_bottom"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:gravity="center"
    android:minHeight="70dp" >

    <Button
        android:id="@+id/button_update"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:background="@drawable/btn_update" >
    </Button>
</LinearLayout>

1 个答案:

答案 0 :(得分:3)

在相关活动的清单中添加以下行

android:windowSoftInputMode="adjustPan"