晚安!
我正在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);
}
}
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>
您可以简单地使用单独的标志(带条件的布尔变量)并为软键盘设置检查条件(使用预定义标志,例如true或false)。
在主要活动中。
@Override
protected void onResume() {
super.onResume();
// enter some conditions here
}
当应用程序进入后台onPause和onResume方法将被调用。为了更多的理解,请通过活动生命周期。