SoftKeyboard侦听器,用于检查键盘在ProgressBar正确运行时打开/关闭的时间

问题描述 投票:-4回答:1

晚安!

我正在Android Studio开发一个小应用程序来测试与SoftKeyboard相关的各种功能。

我的应用程序有一个Activity和两个Fragment包含在这个Activity

第二个Fragment是无关紧要的,所以它仍然是空的。

第一个片段包含EditTextRecyclerView(不包含任何数据)和ProgressBar

我正在寻找的行为是当你点击EditText时,SoftKeyboard会打开。此时,如果用户离开应用程序并重新进入,我希望SoftKeyboard保持可见。

否则,如果EditText在应用程序位于前台时没有焦点,我希望SoftKeyboard不可见,当用户关闭并重新打开应用程序时,不应出现SoftKeyboard

另一方面;当用户点击与EditText相关联的确认按钮并通过SoftKeyboard显示时,我希望ProgressBar可见但只是在Activity的水平和垂直中心,而不考虑键盘占用的内容,因为否则这个ProgressBar将从初始位置(键盘打开)移动到最终位置(当SoftKeyboard关闭时),我不想要那种效果。

PD1:请不要将我推荐给其他StackOverflow页面,因为我已经对它们进行了全部分析,但它们都不适用于我。

PD2:不要考虑到我已经将XML格式的文件代码放入图像中。我花了45分钟尝试将代码作为文本,但我一直摆脱缩进错误。

我希望你能提前帮助我,多多谢意。

主要内容:

public class MainActivity extends AppCompatActivity {

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

        ViewPager viewPager = findViewById(R.id.viewPager);
        setupViewPager(viewPager);

        TabLayout TabLayout = findViewById(R.id.tabs);
        TabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new Fragment1(), "Fragment1");
        adapter.addFragment(new Fragment(), "Fragment2");
        viewPager.setAdapter(adapter);
    }
}

片段1:

public class Fragment1 extends Fragment {

    private EditText mSearchEditText;
    private ProgressBar mProgressBar;
    private RecyclerView mRecyclerView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_1, container, false);

        mSearchEditText = view.findViewById(R.id.search_edit_text);
        mSearchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    hideSoftKeyboard(getActivity());
                    mProgressBar.setVisibility(View.VISIBLE);

                    return true;
                }
                return false;
            }
        });

        mRecyclerView = view.findViewById(R.id.recycler_view);
        mProgressBar = view.findViewById(R.id.progress_bar);

        return view;
    }

    public void hideSoftKeyboard(Activity activity) {
        if (activity.getCurrentFocus() == null) {
            return;
        }
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        assert inputMethodManager != null;
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activityRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMaxWidth="0dp"
            app:tabMode="fixed" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

fragment_1.xml:

<?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:orientation="vertical">

    <EditText
        android:id="@+id/search_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter your search"
        android:imeOptions="actionSearch"
        android:inputType="text" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/search_edit_text" />

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="gone" />

</RelativeLayout>
java android xml android-softkeyboard
1个回答
0
投票

您可以简单地使用单独的标志(带条件的布尔变量)并为软键盘设置检查条件(使用预定义标志,例如true或false)。

在主要活动中。

@Override
    protected void onResume() {
        super.onResume();
        // enter some conditions here
    }

当应用程序进入后台onPause和onResume方法将被调用。为了更多的理解,请通过活动生命周期。

enter link description here

© www.soinside.com 2019 - 2024. All rights reserved.