当安卓键盘弹出时如何隐藏特定视图?

4
我将为您翻译以下内容:

我正在尝试编写一段代码,在 Android 键盘显示时隐藏某些 UI 元素。我尝试在清单文件中添加 "adjustResize",但它会自动调整元素大小,而不是隐藏布局中的某些视图。

我的代码:

package com.example.code;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);


    // Checks whether a hardware keyboard is available
    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
        Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
    } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "No above", Toast.LENGTH_SHORT).show();
    }
}  }

XML :

<RelativeLayout
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"
tools:context="com.example.MainActivity">
<ScrollView
    android:id="@+id/scroll"
    android:layout_width="wrap_content"
    android:layout_height="300dp">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/image_back"/>
</ScrollView>

<TextView
    android:id="@+id/xyz"
    android:layout_width="fill_parent"
    android:layout_height="65dp"
    android:layout_alignParentBottom="true"
    android:background="@android:color/holo_orange_dark"
    android:gravity="center_horizontal"
    android:text="Test Layout"
    android:textSize="40sp" />

<EditText
    android:id="@+id/abc"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/xyz"
    android:layout_alignParentLeft="true"
    android:background="@android:color/holo_blue_bright"
    android:ems="10"
    android:gravity="center_horizontal"
    android:text="Please enter text"
    android:textSize="40sp" >

    <requestFocus />
</EditText>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.code"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.code.MainActivity"
        android:windowSoftInputMode="adjustResize"
        android:label="@string/app_name" 
        android:configChanges="orientation|screenSize|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

截图:

enter image description here

这是我的应用程序的上述代码和屏幕截图,我想在键盘打开时隐藏"测试布局"文本视图。那么,请为我的问题提供一些技巧和解决方案。


这个布局里有没有带滚动条的ScrollView? - Ashish Tamrakar
2个回答

2
你可以尝试像这样做,并根据键盘的可见性更新布局的可见性。
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);


    // Checks whether a hardware keyboard is available
    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
        Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
    } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
    }
}

1
同样的方式,您也可以在Fragment中重写onConfigurationChanged方法。 - Catalina
但是我尝试了你的代码,即使我在清单文件中的活动中给出了android:configChanges="orientation|keyboardHidden|screenSize",onConfigurationChanged也没有被调用。 - MyCode
1
这可能有很多原因。您可以查看此链接以获取一些关于为什么未被调用的想法。 - Catalina
是的,我已经查看了那篇帖子,但是我已经应用了所有的内容,请仔细阅读我的更新代码...你会更好地了解的。 - MyCode
2
是的,当我改变设备方向(从竖屏到横屏)时,该方法会被调用。但是,当我按下EditText并弹出键盘时,我该如何调用它?此时,我需要检查键盘是否打开。 - MyCode
不知道为什么它不起作用。或者,当您的EditText获得/失去焦点时,您可以隐藏/显示它。<your-edit-text>.setOnFocusChangeListener(...); - Catalina

0
/*Hide button when keyboard is open*/
view.getViewTreeObserver().addOnGlobalLayoutListener(new 
ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
    Rect r = new Rect();
    view.getWindowVisibleDisplayFrame(r);

    int heightDiff = view.getRootView().getHeight() - (r.bottom - r.top);

    if (heightDiff > 244) { // if more than 100 pixels, its probably a keyboard...
        //ok now we know the keyboard is up...
        buttonLayout.setVisibility(View.GONE);


    } else {
        //ok now we know the keyboard is down...
        buttonLayout.setVisibility(View.VISIBLE);


    }
}
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接