花了我一段时间才弄清楚这个setOnEditorActionListener是罪魁祸首。至少在该区域附近。 当按下回车键时,在真实设备上运行测试不会触发任何操作。
与仿真器相同,但是使用计算机键盘上的Enter可以正常工作。 我在我的.xml文件中添加了一些用户在此处建议的行。
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
mFilters = (ImageView) view.findViewById(R.id.ic_search);
mSearchText = (EditText) view.findViewById(R.id.input_search);
Log.d(TAG, "getElasticSearchPassword: retrieving elasticsearch aissa password."+ mElasticSearchPassword);
getElasticSearchPassword();
init();
return view;
}
private void init(){
mSearchText.setOnEditorActionListener(new
TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_SEARCH
||actionId == EditorInfo.IME_ACTION_DONE
|| event.getAction() == KeyEvent.ACTION_DOWN
|| event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
mPosts = new ArrayList<Post>();
Retrofit retrofit = new Retrofit.Builder()
baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Code goes here
}
Return False
}
});
}
这是我的原始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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/search_toolbar"
android:background="@drawable/grey_border_bottom"
android:padding="2dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:hint="search..."
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textSize="14sp"
android:layout_toLeftOf="@+id/ic_search"
android:id="@+id/input_search"
android:layout_gravity="center_vertical"
android:maxLines="1"
android:imeOptions="actionSearch"
android:inputType="text"
/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/ic_search"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_search"
android:layout_marginRight="5dp"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"
android:scrollbars="vertical"
android:layout_below="@+id/search_toolbar"
android:background="@color/lightGrey">
</android.support.v7.widget.RecyclerView>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
android:visibility="gone">
</FrameLayout>
</RelativeLayout>
我也尝试删除if条件。怀疑某些API不响应IME_ACTION。