我在Fragment
中添加了EditText
, 但是即使我点击EditText
, 键盘也不会显示出来。
这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<EditText
android:id="@+id/setting_nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="8"
android:gravity="right"
android:hint="Samantha"
android:textColorHint="@color/nav_selected"
android:textSize="14dp"
android:textColor="@color/nav_selected"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" >
</EditText>
<EditText
android:id="@+id/setting_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="8"
android:gravity="right"
android:hint="Samantha"
android:textColorHint="@color/nav_selected"
android:textSize="14dp"
android:textColor="@color/nav_selected"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" >
</EditText>
</LinearLayout>
我尝试获取EditText
的监听器
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
edit_nickname.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imm.showSoftInput(edit_nickname, 0);
//edit_nickname.requestFocus(); --> fail
//imm.showSoftInput(edit_nickname, InputMethodManager.SHOW_IMPLICIT); --> fail
//imm.showSoftInput(edit_nickname, InputMethodManager.SHOW_FORCED); --> fail
}
});
这段代码未能成功...
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
此外,我尝试添加
LinearLayout
以实现focusableInTouchMode
和focusable
,但失败了。<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:padding="10dp"
android:background="@color/black_transparent"
android:focusableInTouchMode="true"
android:focusable="true">
<EditText .../>
<EditText .../>
</LinearLayout>
如果我使用
<requestFocus />
,那么键盘会弹出,但仅适用于EditText
中的一个。我该怎么办?为什么键盘不显示?
onCreateView
中设置View rootView = inflater.inflate(R.layout.setting, container, false);
和return rootView;
。 - Soyeon Kim