Как написать ключевое событие поиска для edittext внутри макета ящика?

Привет, я делаю приложение в том, что у меня есть панель навигации. Внутри панели навигации я текст для редактирования над списком для поиска. Теперь я хочу написать событие клавиши поиска клавиатуры Android для этого edittext. iused android:imeOptions="actionSearch", поэтому на клавиатуре Android отображается кнопка поиска. когда я нажимаю на поиск, ничего не происходит. Я пробовал код, но он не обнаруживает мой текст редактирования. пожалуйста, помогите мне, если кто-нибудь знает.

Мой 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;
                    }
                });
            }
        };

person user3743407    schedule 16.06.2014    source источник


Ответы (1)


Вы можете использовать набор edittext для actioneditlistener для своих целей. Вот код:

editTextSearch.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // do your stuff here
                imageButtonSearch.performClick();
 //I already had a search button inside edittext so I am using it for getting search results
            }
            return false;
        }
    });
person Illegal Argument    schedule 16.06.2014