如何在按钮单击时关闭键盘?我有一个片段,它有一个EditText和两个按钮.一个提交EditText内容,另一个只是关闭片段.现在当片段消失时,键盘会停留.但是,按后退按钮会关闭键盘或单击"完成"也会关闭它.但我需要的是当片段关闭时键盘消失.
我在这里,这里或这里尝试过类似问题的解决方案,但似乎都没有效果.大多数人扔了一个NullPointerException.一切都是为了活动而不是碎片.调用键盘的代码有效:
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
Run Code Online (Sandbox Code Playgroud)
但是我必须添加getActivity()才能使它工作.
任何帮助将不胜感激.
android android-softkeyboard android-edittext android-fragments
我有一个带有两个按钮的片段 - 确认并取消.一个按钮,即确认按钮,从EditText获取用户输入并将其转换为USSD请求.现在,我想让取消按钮关闭该片段,就像alertDialog上的否定按钮一样.
我读过很多与此类似的问题,但似乎没有一个问题满足我的需要.
public void onClick(View v) {
switch (v.getId()) {
case R.id.confirmButton:
if (inputField.getText().toString().trim().length() == 16) {
load();
inputField.requestFocus();
} else if (inputField.getText().toString().trim().length() < 16) {
Toast.makeText(getActivity(), R.string.toast_number_not_valid, Toast.LENGTH_SHORT).show();
}
break;
case R.id.cancelButton:
dismiss();
break;
}
}
private void load() {
// bla - bla - bla
}
public void dismiss(){
//dear fragment, I don't need you right now, just dismiss
}
Run Code Online (Sandbox Code Playgroud)
这里我需要dismiss()方法.
任何帮助将不胜感激.
我有一个从活动启动的片段。现在我注意到,当触摸空白区域时,父活动也会被触摸。仅存在不受影响的按钮的区域。它的作用就好像它是透明的一样。
有办法防止这种情况吗?
父活动 XML:
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@id/layout_dashboard_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/cardview_light_background">
<ScrollView
android:id="@id/main_scroll_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:fillViewport="true">
<LinearLayout
android:id="@id/layout_dashboard_scroll_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@id/dashboard_alert_section"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
<include layout="@layout/dashboard_feature_tip_item" />
</FrameLayout>
<LinearLayout
android:id="@id/main_control_layout"
android:layout_width="fill_parent"
android:layout_height="611dp">
<LinearLayout
android:id="@id/layout_main"
style="@style/CardView.Dark"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/c_main_back_ground_color"
android:gravity="bottom"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/layout_balance_check"
android:layout_width="fill_parent"
android:layout_height="120.0dip"
android:layout_marginBottom="6.0dip"
android:layout_marginEnd="@dimen/dashboard_feature_end_margin_usa"
android:layout_marginLeft="@dimen/dashboard_feature_start_margin_usa"
android:layout_marginRight="@dimen/dashboard_feature_end_margin_usa"
android:layout_marginStart="@dimen/dashboard_feature_start_margin_usa"
android:layout_marginTop="@dimen/dashboard_feature_top_margin_usa"
android:layout_weight="1.0"
android:background="@color/battery_charging_icon_color"
android:clickable="true"
android:elevation="@dimen/dashboard_feature_bg_elevation"
android:focusable="true"
android:focusableInTouchMode="false"
android:theme="@style/Base.Theme.AppCompat"
tools:targetApi="lollipop">
<include
layout="@layout/sub_layout_account_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:id="@+id/view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="?android:selectableItemBackground"
android:duplicateParentState="true" /> …Run Code Online (Sandbox Code Playgroud)