【问题标题】:How to hide certain view while android keyboard is shown?如何在显示 android 键盘时隐藏某些视图?
【发布时间】:2015-06-21 07:44:41
【问题描述】:

我正在尝试编写代码以在显示 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>

截图:

所以,这些是上面的代码和我的应用程序的屏幕截图,所以我想在键盘打开时隐藏“Test Layout”文本视图。所以,请为我的问题推荐一些技术和解决方案。

【问题讨论】:

  • 有这个布局的滚动视图吗?

标签: android keyboard android-edittext


【解决方案1】:

您可以尝试这样的操作,并根据键盘的可见性更新布局的可见性。

@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();
    }
}

【讨论】:

  • 同理。您也可以在 Fragment 中覆盖 onConfigurationChanged 方法
  • 但我尝试了你的代码,但即使我在清单中的活动中给出了 android:configChanges="orientation|keyboardHidden|screenSize" 也没有调用 onConfigurationChanged
  • 它可以是很多东西。您可以查看this,了解有关为什么不调用它的一些想法。
  • 是的,我已经检查了那个帖子,但是我应用了所有内容,请查看我更新的代码..您可以正确了解。
  • 是的方法被调用,当我改变设备的方向意味着纵向到横向但是当我按下edittext然后键盘弹出时我怎么能调用,那时我需要检查是否键盘是否打开。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 2019-07-23
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多