嗨,我正在做一个应用程序,因为我有导航抽屉。在导航抽屉内,我是用于搜索的列表视图上方的编辑文本。现在我想为那个edittext编写android键盘搜索键事件。使用过 android:imeOptions="actionSearch"
,所以它在安卓键盘上显示搜索按钮。当我点击搜索时,没有任何反应。我尝试了一些代码,但它没有检测到我的编辑文本。如果有人知道,请帮助我。
我的xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/leadna_home_bg"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:src="@drawable/mainbtns3" />
<Button
android:id="@+id/btnTasks"
android:layout_width="110dp"
android:layout_height="105dp"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignLeft="@+id/btnLeads"
android:layout_marginBottom="26dp"
android:background="@android:color/transparent" />
<Button
android:id="@+id/btnNearMe"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_above="@+id/btnTasks"
android:layout_alignLeft="@+id/imageView1"
android:background="@android:color/transparent" />
<Button
android:id="@+id/btnAddLeads"
android:layout_width="105dp"
android:layout_height="90dp"
android:layout_alignBaseline="@+id/btnNearMe"
android:layout_alignBottom="@+id/btnNearMe"
android:layout_alignParentRight="true"
android:background="@android:color/transparent" />
<Button
android:id="@+id/btnAppointments"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_alignTop="@+id/btnTasks"
android:layout_toLeftOf="@+id/btnTasks"
android:background="@android:color/transparent" />
<Button
android:id="@+id/btnLeads"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_above="@+id/btnAddLeads"
android:layout_alignRight="@+id/imageView1"
android:layout_marginRight="47dp"
android:background="@android:color/transparent" />
<Button
android:id="@+id/btnSync"
android:layout_width="100dp"
android:layout_height="95dp"
android:layout_alignBottom="@+id/btnLeads"
android:layout_alignRight="@+id/btnAppointments"
android:background="@android:color/transparent"
android:onClick="showActionSheet" />
<Button
android:id="@+id/btnSearch"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignBaseline="@+id/btnNearMe"
android:layout_alignBottom="@+id/btnNearMe"
android:layout_centerHorizontal="true"
android:background="@android:color/transparent"/>
</RelativeLayout>
</FrameLayout>
<!-- The navigation drawer -->
<LinearLayout
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/darker_gray"
android:layout_gravity="start" >
<EditText
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textSize="20sp"
android:hint="Search"
android:imeOptions="actionSearch"
android:imeActionLabel="Search"
android:drawableRight="@drawable/ic_action_search" >
</EditText>
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
我的代码:
opcionesMenu = new String[] { "Home", "Logout" };
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.left_drawer);
mDrawer = (LinearLayout) findViewById(R.id.drawer);
drawerList.setAdapter(new ArrayAdapter<String>(getActionBar()
.getThemedContext(), android.R.layout.simple_list_item_1,
opcionesMenu));
drawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0:
break;
case 1:
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.clear();
prefsEditor.commit();
finish();
System.exit(0);
break;
}
}
});
tituloSeccion = getTitle();
tituloApp = getTitle();
drawerLayout.setDrawerListener(drawerToggle);
getActionBar().setCustomView(R.layout.custom_action_bar);
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_navigation_drawer, R.string.app_name,
R.string.app_name) {
public void onDrawerClosed(View view) {
// getActionBar().setTitle(tituloSeccion);
invalidateOptionsMenu();
EditText searchBar = (EditText) findViewById(R.id.searchView);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchBar.getWindowToken(), 0);
}
public void onDrawerOpened(View view) {
// getActionBar().setTitle(tituloApp);
invalidateOptionsMenu();
final EditText editText = (EditText) findViewById(R.id.searchView);
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(MainActivity.this,
editText.getText(), Toast.LENGTH_LONG)
.show();
return true;
}
return false;
}
});
}
};