簡體   English   中英

SoftKeyboard Listener 用於檢查鍵盤何時打開/關閉而 ProgressBar 正確運行

[英]SoftKeyboard Listener to check when the keyboard opens / closes with ProgressBar running correctly

晚安!

我正在Android Studio開發一個小應用程序來測試與SoftKeyboard相關的各種功能。

我的應用程序有一個Activity和兩個Fragment包含在這個Activity

第二個Fragment無關緊要,因此它保持為空。

第一個片段包含一個EditText 、一個RecyclerView (不包含任何數據)和一個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);
    }
}

活動_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>

片段_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>

您可以簡單地使用單獨的標志(帶條件的布爾變量)並為軟鍵盤放置檢查條件(使用預定義的標志,例如真或假)。

在主要活動中。

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

隨着應用程序進入后台,onPause 和 onResume 方法將被調用。 如需更多了解,請查看活動生命周期。

在此處輸入鏈接描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM