【问题标题】:android - How to shift EditText to top when focused?android - 聚焦时如何将 EditText 移到顶部?
【发布时间】:2017-03-27 18:24:55
【问题描述】:

我有一个视图,其中包含顶部的 ImageView 和下方带有按钮的 EditText。 当键盘出现时,如何将 ImageView 移出屏幕以允许显示 EditText 和 Button?

这是我的 layout.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    tools:context="com.archimedia.appToYours.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:ignore="UselessParent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="1">


            <!-- this is what I want to shift up out of the screen -->
            <ImageView
                android:id="@+id/upToYoursImageView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:contentDescription=""
                app:srcCompat="@drawable/_title"/>

            <EditText
                android:id="@+id/code"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/targa"
                android:ems="15"
                android:fontFamily="sans-serif-condensed"
                android:hint="Some text..."
                android:inputType="textCapCharacters"
                android:maxLength="15"
                android:paddingLeft="60dp"
                android:textColor="#222"
                android:textColorHint="#222"
                android:textColorLink="@android:color/holo_blue_bright"
                android:textColorSecondary="#222"
                android:textCursorDrawable="@drawable/cursor"
                android:textSize="40sp"
                android:textStyle="bold"
                tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="130dp"
                android:layout_marginBottom="50dp"
                android:layout_weight="0.50"
                tools:ignore="InefficientWeight">

                <Button
                    android:id="@+id/send_button"
                    style="@style/Widget.AppCompat.Button.Colored"
                    android:layout_width="170dp"
                    android:layout_height="170dp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:background="@drawable/rounded_button"
                    android:foregroundGravity="center_vertical|center_horizontal"
                    android:text="Go!"
                   android:textAppearance="@style/TextAppearance.AppCompat.Display3"
                    android:textSize="38sp"
                    tools:ignore="HardcodedText" />
            </RelativeLayout>
          </LinearLayout>
       </LinearLayout>
    </RelativeLayout>

【问题讨论】:

    标签: java android xml android-layout android-studio-2.2


    【解决方案1】:

    我也有类似的要求。我解决它的方法是在根视图上监听 LayoutChanged 事件。一旦我看到视图的高度变小了,我就认为键盘打开并做出了相应的反应。 这是该方法的示例。希望对您有所帮助:

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        findViewById(R.id.activity_main).addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (bottom < oldBottom) {
                    // View got smaller in height
                    // In this case this means that the keyboard was opened
                    findViewById(R.id.upToYoursImageView).setVisibility(View.GONE);
                } else {
                    // View got larger in height
                    findViewById(R.id.upToYoursImageView).setVisibility(View.VISIBLE);
                }
            }
        });
    }
    

    }

    AndroidManifest.xml

    <activity android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize"/>
    

    【讨论】:

    • 它只是在键盘出现时隐藏ImageView,但EditTextView没有出现,并且Button仍然没有显示
    • 您也可以将其添加到滚动视图中,并在打开键盘后滚动到底部。 -> scroll.post(new Runnable() { @Override public void run() { scroll.fullScroll(View.FOCUS_DOWN); } });
    【解决方案2】:

    我认为你可以添加一个 ScrollView 试试这个代码,我希望它有帮助

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        tools:context="com.archimedia.appToYours.MainActivity">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:ignore="UselessParent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:weightSum="1">
    
    
                <!-- this is what I want to shift up out of the screen -->
                <ScrollView android:layout_height="wrap_content"
                android:layout_width="match_parent">
                <ImageView
                    android:id="@+id/upToYoursImageView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:contentDescription=""
                    app:srcCompat="@drawable/_title"/>
    
                <EditText
                    android:id="@+id/code"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/targa"
                    android:ems="15"
                    android:fontFamily="sans-serif-condensed"
                    android:hint="Some text..."
                    android:inputType="textCapCharacters"
                    android:maxLength="15"
                    android:paddingLeft="60dp"
                    android:textColor="#222"
                    android:textColorHint="#222"
                    android:textColorLink="@android:color/holo_blue_bright"
                    android:textColorSecondary="#222"
                    android:textCursorDrawable="@drawable/cursor"
                    android:textSize="40sp"
                    android:textStyle="bold"
                    tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" />
                </ScrollView>
                
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="130dp"
                    android:layout_marginBottom="50dp"
                    android:layout_weight="0.50"
                    tools:ignore="InefficientWeight">
    
                    <Button
                        android:id="@+id/send_button"
                        style="@style/Widget.AppCompat.Button.Colored"
                        android:layout_width="170dp"
                        android:layout_height="170dp"
                        android:layout_alignParentTop="true"
                        android:layout_centerHorizontal="true"
                        android:background="@drawable/rounded_button"
                        android:foregroundGravity="center_vertical|center_horizontal"
                        android:text="Go!"
                       android:textAppearance="@style/TextAppearance.AppCompat.Display3"
                        android:textSize="38sp"
                        tools:ignore="HardcodedText" />
                </RelativeLayout>
              </LinearLayout>
           </LinearLayout>
        </RelativeLayout>

    【讨论】:

    • 对不起,我在 layout_width="match_parent" 上做错了尝试删除它并尝试一下,至少我试过了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 2020-11-24
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    相关资源
    最近更新 更多