如何在Android中的片段中完美展示软键盘?

栏目: Android · 发布时间: 5年前

内容简介:我在Activity中的Fragment中有一个EditText.我的活动布局:AndroidManifest.xml中的我的Activity配置:

我在Activity中的Fragment中有一个EditText.

我的活动布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/login_bg">
    ...

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    ...

</RelativeLayout>

AndroidManifest.xml中的我的Activity配置:

<activity
        android:name="com.demo.LoginActivity"
        android:configChanges="orientation|keyboardHidden"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/activityTheme" />

用于在Activity中启动片段的代码:

private void startFragment(BaseFragment fragment, String tag) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.fragment_container, fragment);
    fragmentTransaction.addToBackStack(tag);
    fragmentTransaction.commitAllowingStateLoss();
}

我的片段布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/common_background_color_white"
    android:orientation="vertical"
    android:clickable="true"
    android:paddingLeft="@dimen/email_common_padding_horizontal"
    android:paddingRight="@dimen/email_common_padding_horizontal">

    ...

    <com.example.widget.LineEditView
        android:id="@+id/login_email_input"
        style="@style/BaseEditText.LoginEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
    />

    ...

</LinearLayout>

我的自定义小部件LineEditView是一个子类扩展RelativeLayout,它的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:gravity="start|center_vertical"
        android:background="@android:color/transparent"
        android:textColor="@color/common_text_color_black"
        android:maxLines="1"
        android:textCursorDrawable="@drawable/common_cursor_background_orange"
        android:textSize="@dimen/email_fields_text_size"
        android:paddingBottom="@dimen/email_fields_text_padding_bottom"/>

    <View
        android:id="@+id/underline"
        android:layout_below="@id/edit"
        android:layout_width="match_parent"
        android:layout_height="2px"/>
</RelativeLayout>

我想根据EditText的inputType属性显示软键盘,并且可以轻松隐藏.

我尝试过但不工作或不完美:

1.根据 Show keyboard for edittext when fragment starts 可以显示软键盘但不能轻易隐藏(有时甚至无法隐藏)并且它根据EditText的inputType属性显示键盘.

2.我将以下代码添加到My Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container) {
    mEditText = (EditText) rootView.findViewById(R.id.edit);
    mEditText.requestFocus();
    mEditText.setFocusable(true);
}

@Override
public void onResume() {
    mEditText.postDelayed(mShowSoftInputRunnable, 400);
    super.onResume();
}

private Runnable mShowSoftInputRunnable = new Runnable() {
    @Override
    public void run() {
        FragmentActivity activity = getActivity();
        if (activity == null)
            return;

        InputMethodManager input = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        input.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
    }
};

但它在我的片段中根本无法显示软键盘.

我不能将EditText放到Activity中,因为它需要重构很多代码.

有没有人有想法解决这个问题?


以上所述就是小编给大家介绍的《如何在Android中的片段中完美展示软键盘?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Head First Servlets & JSP(中文版)

Head First Servlets & JSP(中文版)

(美)巴萨姆、(美)塞若、(美)贝茨 / 苏钰函、林剑 / 中国电力出版社 / 2006-10 / 98.00元

《Head First Servlets·JSP》(中文版)结合SCWCD考试大纲讲述了关于如何编写servlets和JSP代码,如何使用JSP表达式语言,如何部署Web应用,如何开发定制标记,以及会话状态、包装器、过滤器、企业设计模式等方面的知识,以一种轻松、幽默而又形象的方式让你了解、掌握servlets和JSP,并将其运用到你的项目中去。《Head First Servlets·JSP》(中......一起来看看 《Head First Servlets & JSP(中文版)》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具