Mam EditText wewnątrz fragmentu wewnątrz działania.Jak pokazać miękką klawiaturę doskonale w fragmencie w systemie Android?
Mój układ aktywny:
<?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>
Mój config aktywny w AndroidManifest.xml:
<activity
android:name="com.demo.LoginActivity"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/activityTheme" />
Mój kod to wykorzystać, aby rozpocząć fragment aktywności:
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();
}
swojego układu Fragment :
<?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>
Moja klienta Widget LineEditView
jest klasą dziecko przedłużyć RelativeLayout
, a układ:
<?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>
chcę pokazać klawiaturę według własności InputType z EditText i można łatwo ukryć.
Co próbowałem ale nie działa lub nie jest doskonały:
1. Zgodnie Show keyboard for edittext when fragment starts może pokazać klawiaturę, ale nie da się ukryć łatwo (nawet niekiedy nie da się ukryć) i pokazać klawiaturę niezgodnie z własności InputType Edytować tekst.
2.I Dodaj następujący kod do Mojego 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);
}
};
ale nie może wyświetlić klawiaturę w ogóle w moim fragmentu.
Nie mogę wstawić EditText do działania, ponieważ musi on refaktoryzować dużo kodu.
Czy ktoś ma pomysły na rozwiązanie tego problemu?
Dziękuję, twoja odpowiedź bardzo mi pomogła. – Waylent
Wielkie dzięki. Ten działa doskonale! próbowałem wszystkich innych rzeczy !! nie działało tak ładnie !! –
Oboje jesteście mile widziane. Ten kod jest najlepszym kodem używanym do pokazywania lub ukrywania miękkiej klawiatury w systemie Android. – apmartin1991