显示android键盘时如何隐藏特定视图?
我试图编写一个代码来隐藏某些ui元素,当Android键盘显示时,我试图在清单中添加“adjustResize”,但它会自动重新调整元素的大小,但不会隐藏布局中的一些视图 MyCode:显示android键盘时如何隐藏特定视图?,android,keyboard,android-edittext,Android,Keyboard,Android Edittext,我试图编写一个代码来隐藏某些ui元素,当Android键盘显示时,我试图在清单中添加“adjustResize”,但它会自动重新调整元素的大小,但不会隐藏布局中的一些视图 MyCode: package com.example.code; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void on
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();
}
} }
<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>
XML:
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();
}
} }
<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>
清单:
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();
}
} }
<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>
屏幕截图:
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();
}
} }
<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>
以上是我应用程序的代码和屏幕截图,所以我想在键盘打开时隐藏“测试布局”“文本视图。因此,请为我的问题提供一些技巧和解决方案。您可以尝试类似的方法,并根据键盘的可见性更新布局的可见性
@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();
}
}
同样的方式。您可以在片段中重写onConfigurationChanged方法,但我尝试了您的代码,但onConfigurationChanged没有被调用,即使我在Manifests中的活动中给出了android:configChanges=“orientation | keyboardHidden | screenSize”,它可以是很多东西。你可以查看一些关于为什么不调用它的想法。是的,我已经查看了那篇文章,但我应用了所有内容,请查看我更新的代码。。您可以正确地了解。是的,当我更改设备方向时,会调用该方法,这意味着从纵向到横向,但当我按下编辑文本时,键盘会弹出,此时我需要检查键盘是否打开。此布局是否有滚动视图?