我的Layout上有一个EditText,它是一个Framelayout,问题是当我在EditText中输入一些数据然后我按下手机上的后退按钮来隐藏键盘时,它会一直显示键盘的阴影直到我按下活动的任何地方。
我如何解决这个问题。
按下后退按钮隐藏键盘后会出现这种情况,我必须再次按下活动才能使黑色阴影消失。(检查底部的黑色阴影)
的AndroidManifest.xml
<activity
android:name="com.example.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize|screenLayout"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" >
</activity>
activty_main.xml
<com.example.layout.MainLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/activity_main_content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout></com.example.layout.MainLayout>
更新: - MainActivity.Java
public class MainActivity extends FragmentActivity {
// The MainLayout which will hold both the sliding menu and our main content
// Main content will holds our Fragment respectively
MainLayout mainLayout;
// ListView menu
private ListView lvMenu;
private String[] lvMenuItems;
// Menu button
TextView btMenu;
// Title according to fragment
TextView tvTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate the mainLayout
mainLayout = (MainLayout)this.getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(mainLayout);
lvMenuItems = getResources().getStringArray(R.array.menu_items);
lvMenu = (ListView) findViewById(R.id.activity_main_menu_listview);
lvMenu.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, lvMenuItems));
lvMenu.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
onMenuItemClick(parent, view, position, id);
}
});
// Get menu button
btMenu = (TextView) findViewById(R.id.activity_main_content_button_menu);
btMenu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Show/hide the menu
toggleMenu(v);
}
});
// Get title textview
tvTitle = (TextView) findViewById(R.id.activity_main_content_title);
// Add FragmentMain as the initial fragment
FragmentManager fm = MainActivity.this.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentHome fragment = new FragmentHome();
ft.add(R.id.activity_main_content_fragment, fragment);
ft.commit();
}
public void toggleMenu(View v){
mainLayout.toggleMenu();
}
// Perform action when a menu item is clicked
private void onMenuItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = lvMenuItems[position];
String currentItem = tvTitle.getText().toString();
// Do nothing if selectedItem is currentItem
if(selectedItem.compareTo(currentItem) == 0) {
mainLayout.toggleMenu();
return;
}
FragmentManager fm = MainActivity.this.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment fragment = null;
String[] menu = getResources().getStringArray(R.array.menu_items);
if(selectedItem.compareTo(menu[0]) == 0) {
fragment = new FragmentHome();
} else if(selectedItem.compareTo(menu[1]) == 0) {
fragment = new FragmentSettings();
}
if(fragment != null) {
// Replace current fragment by this new one
ft.replace(R.id.activity_main_content_fragment, fragment);
ft.commit();
// Set title accordingly
tvTitle.setText(selectedItem);
}
// Hide menu anyway
mainLayout.toggleMenu();
}
@Override
public void onBackPressed() {
if (mainLayout.isMenuShown()) {
mainLayout.toggleMenu();
}
else {
super.onBackPressed();
}
}
@Override
public void onConfigurationChanged(Configuration config){
super.onConfigurationChanged(config);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
答案 0 :(得分:0)
最后我做到了。 我不得不添加我的片段
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(MYeditText.getWindowToken(), 0);
答案 1 :(得分:0)
我遇到了同样的问题,我将下面的代码放在MainActivity OF Frame Layout
中解决了 <activity
android:name="com.shane.pir.carhire.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize|screenLayout"
android:windowSoftInputMode="adjustPan" >
</activity>