将addtextchangedlistener用于NavigationView Edittext

时间:2015-11-04 03:31:41

标签: android xml android-edittext navigationview

我正在尝试在navigationView edittext中添加addtextchangedlistener事件。

我想添加它,所以每当我在文本框中输入字符时,它会自动捕获文本,然后使用提供的文本过滤我的菜单。

当我运行应用程序时会发生什么,它会抛出一个空指针异常 addTextChangedListener。如何修复此问题以允许它检测输入并从那里控制它?

这是我的xml和类文件:

nav_header_home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"
    android:background="#e3e4e5"
    android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical" >

      <EditText
            android:layout_width="match_parent"
            android:layout_height="@dimen/search_bar_layout_height"
            android:id="@+id/editTextSearchBar"
            android:background="@drawable/rounded_edittext"
            android:hint="Search"
            android:textColorHint="@color/colorSearchBarHint"
            android:textAlignment="center"
            android:drawableLeft="@drawable/ic_menu_search"

            android:textColor="@color/colorProfileName"
           />
        </LinearLayout>
    <LinearLayout

activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:fitsSystemWindows="true" tools:openDrawer="start">

    <include layout="@layout/app_bar_home" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView android:id="@+id/nav_view"
        android:layout_width="wrap_content" android:layout_height="match_parent"
        android:layout_gravity="start" android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_home" app:menu="@menu/activity_home_drawer" />

</android.support.v4.widget.DrawerLayout>

我的HomeActivity类:

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ListView;

import com.app.test.R;

public class HomeActivity extends   AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener   {

    int mProfileMenuCtr = 0;
    String [] mValues = {"Inbox","News","My Profile"};
    EditText mSearchBar;
    String mTempText;
    int [] mTempCtr;
    int mTempCtr2;
    MenuItem mItem;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        mSearchBar = (EditText)findViewById(R.id.editTextSearchBar);

        mSearchBar.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mSearchBar.setError(null);
            }

            @Override
            public void afterTextChanged(Editable s) {
                mTempText = mSearchBar.toString();
                mTempCtr2 = 0;
                for(int o = 0;o <mValues.length;o++){

                    boolean containerContainsContent = mValues[o].toLowerCase().contains(mTempText.toLowerCase());
                    if(containerContainsContent)
                    {
                        mTempCtr2++;
                        mTempCtr [mTempCtr2] = o;

                    }
                }
                invalidateOptionsMenu();

            }
        });


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });



        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();


    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_home_drawer, menu);

        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu){
            getMenuInflater().inflate(R.menu.activity_home_drawer, menu);
        hideMenuItems(menu, false);

        return super.onPrepareOptionsMenu(menu);
    }

    private void hideMenuItems(Menu menu, boolean visible)
    {
        for(int i = 0; i < menu.size(); i++){

            menu.getItem(i).setVisible(visible);

        }
        for(int ii = 0; ii < mTempCtr.length;ii++){
            menu.getItem(ii).setVisible(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_inbox) {

        } else if (id == R.id.nav_news) {

        } else if (id == R.id.nav_my_profile) {

    }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

回答为时已晚,但这是我的回答

查找导航标题,然后找到EditText

View header = navigationView.getHeaderView(0);
mSearchBar = (EditText) header.findViewById(R.id. editTextSearchBar);

然后在EditText

上添加 TextWatcher