Key board appears every time when it open the home activity of my app,UNABLE to HIDE KEYBOARD

删除回忆录丶 提交于 2019-12-11 18:54:12

问题


I have used ACTION BAR and a SEARCH VIEW in it.Every time i open the slide bar or open home activity it shows the keyboard. I think it is setting the focus on search View of action bar.But i'm unable to unfocused it. I tried this code to hide but it dint worked.\

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

And have also tried this

Here is the code...

public class MainActivity extends FragmentActivity implements SearchView.OnQueryTextListener , DrawerLayout.DrawerListener,View.OnTouchListener{
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
public SearchView mSearchView;
private CharSequence mDrawerTitle;
String selectedItem;
private CharSequence mTitle;
private MatrixCursor cursor;
private ArrayList<String> list = new ArrayList<String>();
private String[] navMenuTitles;
private TypedArray navMenuIcons;
public static Boolean flag = false;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;   
MenuItem searchItem;
public static String state;
public static Boolean search_flag; 
@Override
protected void onCreate(Bundle savedInstanceState) 

{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    mTitle = mDrawerTitle = getTitle();
    search_flag=true;
    // load slide menu items
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

    // nav drawer icons from resources
    navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

    navDrawerItems = new ArrayList<NavDrawerItem>();

    // adding nav drawer items to array
    // Home
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
    // Find People
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
    // Photos
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
    // Communities, Will add a counter here
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
    // Pages
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
    // What's hot, We  will add a counter here
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);




    // Recycle the typed array
    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    // setting the nav drawer list adapter
    adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
    mDrawerList.setAdapter(adapter);

    // enabling action bar app icon and behaving it as toggle button
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);


    /*final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.action_bar,null);
    getActionBar().setCustomView(actionBarLayout);*/
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, //nav menu toggle icon
            R.string.app_name, // nav drawer open - description for accessibility
            R.string.app_name // nav drawer close - description for accessibility
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        // on first time display view for first nav item
        displayView(0);
    }



    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
       .detectCustomSlowCalls() 
       .detectDiskReads()
       .detectDiskWrites()
       .detectNetwork()
       .penaltyLog()
       .penaltyFlashScreen() 
       .build());
    try{


       StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
       .detectLeakedSqlLiteObjects()
       .detectLeakedClosableObjects() 
       .setClassInstanceLimit(Class.forName("com.shabdkosh.MainActivity"), 100)
       .penaltyLog()
       .build());}
    catch(Exception e){e.printStackTrace();}

    }

/**
 * Slide menu item click listener
 * */
private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // display view for selected nav drawer item
        displayView(position);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.main, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchItem = menu.findItem(R.id.action_search).setVisible(true);
    mSearchView = (SearchView) searchItem.getActionView();
    mSearchView.setQueryHint(getResources().getString(R.string.search_hint));
    mSearchView.setIconifiedByDefault(false);
    mSearchView.setSubmitButtonEnabled(false);
    ((SearchView) searchItem.getActionView()).setIconified(true);

    mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    mSearchView.setFocusable(false);
    searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    if(search_flag){
    mSearchView.setOnQueryTextListener(this);}
    search_flag=false;
    return true;
}

public void hideSoftKeyboard(Activity activity) 
{
    InputMethodManager inputMethodManager = (InputMethodManager)activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    mSearchView.setFocusable(false);
    if(item.getItemId()==R.id.Hindi||item.getItemId()==R.id.English||item.getItemId()==R.id.home){
        mSearchView.setFocusable(false);
    }
    //search_flag=true;

    // toggle nav drawer on selecting action bar app icon/title
            if (mDrawerToggle.onOptionsItemSelected(item))
            {
                search_flag=true;
                //return true;
            }
            // Handle action bar actions click
            if(state.equals("home"))
            {
            switch (item.getItemId())
            {
            case R.id.Hindi:
                if (!Locale.getDefault().toString().equalsIgnoreCase("hi")) 
                {
                    String languageToLoad = "hi"; // language
                    Locale locale = new Locale(languageToLoad);
                    Locale.setDefault(locale);
                    Configuration config = new Configuration();
                    config.locale = locale;
                    getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());


                    Intent in=new Intent(getBaseContext(), MainActivity.class);
                    startActivity(in);
                    finish();


                } else 
                {
                    Toast.makeText(getApplicationContext(),getResources().getString(R.string.hin_sel), Toast.LENGTH_LONG).show();
                }
                return true;
            case R.id.English:
                if (!Locale.getDefault().toString().equalsIgnoreCase("en"))
                {
                    String languageToLoad = "en"; // language
                    Locale locale = new Locale(languageToLoad);
                    Locale.setDefault(locale);
                    Configuration config = new Configuration();
                    config.locale = locale;
                    getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());


                    Intent in=new Intent(getBaseContext(), MainActivity.class);
                    startActivity(in);
                    finish();

                } else {
                    Toast.makeText(getApplicationContext(),getResources().getString(R.string.Al_Eng_sel), Toast.LENGTH_LONG).show();
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
        }else if(state.equals("quiz"))
        {
            switch (item.getItemId())
            {
            case R.id.Hindi:

            if (R.id.Hindi == item.getItemId())

            {
            String languageToLoad = "hi"; // language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new Word_quiz_game()).commit();


            }
            else
            {
            Toast.makeText(getApplicationContext(),getResources().getString(R.string.hin_sel), Toast.LENGTH_LONG).show();
            }

            return true;
            case R.id.English:

            if (R.id.English == item.getItemId())
            {
            String languageToLoad = "en"; // language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;

            getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new Word_quiz_game()).commit();

            }
            else
            {
            Toast.makeText(getApplicationContext(),getResources().getString(R.string.Al_Eng_sel), Toast.LENGTH_LONG).show();
            }

            return true;
            default:
            return super.onOptionsItemSelected(item);
            }


            }

            else if(state.equals("wod"))
            {
            switch (item.getItemId())
            {
            case R.id.Hindi:

            if (R.id.Hindi == item.getItemId())

            {
            String languageToLoad = "hi"; // language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new WordOFday_More()).commit();


            }
            else
            {
            Toast.makeText(getApplicationContext(),getResources().getString(R.string.hin_sel), Toast.LENGTH_LONG).show();
            }

            return true;
            case R.id.English:

            if (R.id.English == item.getItemId())
            {
            String languageToLoad = "en"; // language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;

            getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new WordOFday_More()).commit();

            }
            else
            {
            Toast.makeText(getApplicationContext(),getResources().getString(R.string.Al_Eng_sel), Toast.LENGTH_LONG).show();
            }

            return true;
            default:
            return super.onOptionsItemSelected(item);
            }
            }

            else if(state.equals("browse"))
            {
            switch (item.getItemId())
            {
            case R.id.Hindi:

            if (R.id.Hindi == item.getItemId())

            {
            String languageToLoad = "hi"; // language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new Browse()).commit();


            }
            else
            {
            Toast.makeText(getApplicationContext(),getResources().getString(R.string.hin_sel), Toast.LENGTH_LONG).show();
            }

            return true;
            case R.id.English:

            if (R.id.English == item.getItemId())
            {
            String languageToLoad = "en"; // language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;

            getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new Browse()).commit();

            }
            else
            {
            Toast.makeText(getApplicationContext(),getResources().getString(R.string.Al_Eng_sel), Toast.LENGTH_LONG).show();
            }

            return true;
            default:
            return super.onOptionsItemSelected(item);
            }

            }

            else if(state.equals("his"))
            {
            switch (item.getItemId())
            {
            case R.id.Hindi:

            if (R.id.Hindi == item.getItemId())

            {
            String languageToLoad = "hi"; // language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new His()).commit();


            }
            else
            {
            Toast.makeText(getApplicationContext(),getResources().getString(R.string.hin_sel), Toast.LENGTH_LONG).show();
            }

            return true;
            case R.id.English:

            if (R.id.English == item.getItemId())
            {
            String languageToLoad = "en"; // language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());

            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new His()).commit();

            }
            else
            {
            Toast.makeText(getApplicationContext(),getResources().getString(R.string.Al_Eng_sel), Toast.LENGTH_LONG).show();
            }

            return true;
            default:
            return super.onOptionsItemSelected(item);
            }
            }

            else if(state.equals("fav"))
            {
            switch (item.getItemId())
            {
            case R.id.Hindi:

            if (R.id.Hindi == item.getItemId())

            {
            String languageToLoad = "hi"; // language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new Fav()).commit();


            }
            else
            {
            Toast.makeText(getApplicationContext(),getResources().getString(R.string.hin_sel), Toast.LENGTH_LONG).show();
            }

            return true;
            case R.id.English:

            if (R.id.English == item.getItemId())
            {
            String languageToLoad = "en"; // language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;

            getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new Fav()).commit();

            }
            else
            {
            Toast.makeText(getApplicationContext(),getResources().getString(R.string.Al_Eng_sel), Toast.LENGTH_LONG).show();
            }

            return true;
            default:
            return super.onOptionsItemSelected(item);
            }
            }

            else if(state.equals("qod"))
            {
            switch (item.getItemId())
            {
            case R.id.Hindi:

            if (R.id.Hindi == item.getItemId())

            {
            String languageToLoad = "hi"; // language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new Qod_more()).commit();


            }
            else
            {
            Toast.makeText(getApplicationContext(),getResources().getString(R.string.hin_sel), Toast.LENGTH_LONG).show();
            }

            return true;
            case R.id.English:

            if (R.id.English == item.getItemId())
            {
            String languageToLoad = "en"; // language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());

            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new Qod_more()).commit();

            }
            else
            {
            Toast.makeText(getApplicationContext(),getResources().getString(R.string.Al_Eng_sel), Toast.LENGTH_LONG).show();
            }

            return true;
            default:
            return false;
            }
            }

            return super.onOptionsItemSelected(item);

            }

/* *
 * Called when invalidateOptionsMenu() is triggered
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    try{
    menu.findItem(R.id.Hindi).setVisible(!drawerOpen);
    menu.findItem(R.id.action_search).collapseActionView();
    }
    catch(Exception e){
        e.printStackTrace();
        }
    mSearchView.setFocusable(false);
    hideKeyboard(MainActivity.this);
    getCurrentFocus().clearFocus();
    mDrawerLayout.requestFocus();

    if(state.equals("his")||state.equals("fav")){

    menu.removeItem(R.id.action_search);    
    String lang=Locale.getDefault().toString();
    if (lang.equalsIgnoreCase("en_IN")||lang.equalsIgnoreCase("en_US"))
    {
    menu.removeItem(R.id.English);
    menu.removeItem(R.id.Gujarati);
    menu.removeItem(R.id.Punjabi);
    menu.removeItem(R.id.Bengali);
    menu.removeItem(R.id.Marathi);
    menu.removeItem(R.id.Talugu);
    menu.removeItem(R.id.Tamil);
    menu.removeItem(R.id.Hindi);

    }
    else if(lang.equalsIgnoreCase("hi"))
    {
    menu.removeItem(R.id.Hindi);
    menu.removeItem(R.id.Gujarati);
    menu.removeItem(R.id.Punjabi);
    menu.removeItem(R.id.Bengali);
    menu.removeItem(R.id.Marathi);
    menu.removeItem(R.id.Talugu);
    menu.removeItem(R.id.Tamil);
    menu.removeItem(R.id.English);
    }}

    return super.onPrepareOptionsMenu(menu);
}

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new Qod_more();
        break;
    case 2:
        fragment = new WordOFday_More();
        break;
    case 3:
        fragment = new His();
        break;
    case 4:
        fragment = new Fav();
        break;
    case 5:
        fragment = new Word_quiz_game();
        break;
    case 6:
        fragment = new Browse();
        break;
    case 7:
        Intent in=new Intent(MainActivity.this, Settings.class);
        startActivity(in);
        break;

    default:
        break;
    }

    if (fragment != null) {
        android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager() ;

        android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.replace(R.id.frame_container, fragment);
        fragmentTransaction.commit();

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onQueryTextChange(final String newText) {

    if (newText.length() > 0) {

        try {
            if (newText.length() > 0) {

                if (InternetConnection.isInternetOn(getApplicationContext())) {
                    list = new SearchData().getAutoSuggestion(newText,MainActivity.this);
                } else {
                    list = new SearchDataDB().getAutoSuggestion(newText,MainActivity.this);
                }
            }
            int[] to = { R.id.text_viewdemo };
            String s[] = Arrays.copyOf(list.toArray(),list.toArray().length, String[].class);
            System.out.println("array size :" + s.length);
            String[] columnNames = { "_id", "text" };
            cursor = new MatrixCursor(columnNames);
            String[] array = s; // if strings are in resources
            String[] temp = new String[2];
            int id = 0;
            for (String item : array) {
                temp[0] = Integer.toString(id++);
                temp[1] = item;
                cursor.addRow(temp);
            }
            String[] from = { "text" };
            final CursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.suggest_list, cursor, from, to, 0);
            mSearchView.setSuggestionsAdapter(cursorAdapter);
            mSearchView.setOnSuggestionListener(new OnSuggestionListener() {
                @Override
                public boolean onSuggestionClick(int position) {
                    selectedItem = list.get(position);
                    mSearchView.setQuery(selectedItem, false);
                    if (InternetConnection.isInternetOn(getApplicationContext())) {
                        SearchData_DTO.setSearchData_DTO(new SearchData().getSearched(selectedItem, MainActivity.this));
                    } else {
                        SearchData_DTO.setSearchData_DTO(new SearchDataDB().getSearchedDB(selectedItem, MainActivity.this));
                        }

                    // cursorAdapter.getItem(position).toString();
                    Log.v("search view", selectedItem);                     
                    Intent i = new Intent(MainActivity.this, Search.class);
                    i.putExtra("selected", selectedItem);                       
                    startActivity(i);                       
                    flag=true;
                    return true;
                }

                @Override
                public boolean onSuggestionSelect(int position) {
                    return true;
                }
            });

            // mSearchView.onKeyUp(keyCode, event)

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
    } else {

        // TO REMOVE LIST WHEN NO TEXT IN SEARCHVIEW

        // mSearchView.onActionViewCollapsed();

        if (list != null) {
            if (list.size() > 0) {
                list.clear();
            }
        }
        // mSearchView.setIconified(true);

        int[] to = { R.id.text_viewdemo };
        String s[] = Arrays.copyOf(list.toArray(), list.toArray().length,String[].class);
        System.out.println("array size :" + s.length);
        String[] columnNames = { "_id", "text" };
        cursor = new MatrixCursor(columnNames);
        String[] array = s; // if strings are in resources
        String[] temp = new String[2];
        int id = 0;
        for (String item : array) {
            temp[0] = Integer.toString(id++);
            temp[1] = item;
            cursor.addRow(temp);
        }
        String[] from = { "text" };

        final CursorAdapter cursorAdapter = new SimpleCursorAdapter(this,R.layout.suggest_list, cursor, from, to,0);
        mSearchView.setSuggestionsAdapter(cursorAdapter);
        mSearchView.clearFocus();
        mSearchView.setIconified(false);
        mSearchView.setFocusableInTouchMode(false);

    }
    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
    // TODO Auto-generated method stub
    mSearchView.setVisibility(View.VISIBLE);
    mSearchView.setVisibility(View.INVISIBLE);

    if (InternetConnection.isInternetOn(getApplicationContext())) {
        SearchData_DTO.setSearchData_DTO(new SearchData().getSearched(query, MainActivity.this));
    } else {
        SearchData_DTO.setSearchData_DTO(new SearchDataDB().getSearchedDB(query, MainActivity.this));
    }
    Intent i = new Intent(MainActivity.this, Search.class);
    i.putExtra("selected", query);
    startActivity(i);

    return true;
}


@Override
public void onDrawerClosed(View arg0) {
    // TODO Auto-generated method stub
    hideKeyboard(MainActivity.this);
    mDrawerLayout.requestFocus();
}

@Override
public void onDrawerOpened(View arg0) {
    // TODO Auto-generated method stub      
    hideKeyboard(MainActivity.this);
    mDrawerLayout.requestFocus();
}

@Override
public void onDrawerSlide(View arg0, float arg1) {
    // TODO Auto-generated method stub
    hideKeyboard(MainActivity.this);
    mDrawerLayout.requestFocus();
}

@Override
public void onDrawerStateChanged(int arg0) {
    // TODO Auto-generated method stub
    hideKeyboard(MainActivity.this);
    mDrawerLayout.requestFocus();
}

public  void hideKeyboard(Activity activity) 
{
    InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm != null && activity != null) {
        View currentFocus = activity.getCurrentFocus();

        if (currentFocus != null) {
            imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        }
    }
    mDrawerLayout.requestFocus();
}


@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    mDrawerLayout.requestFocus();
    return false;
}

public void setupUI(View view) {

    if(!(view instanceof SearchView)) {

        view.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                searchItem.collapseActionView();
                return false;
            }
        });
    }

    //If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {

        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

            View innerView = ((ViewGroup) view).getChildAt(i);

            setupUI(innerView);
        }
    }

    if(view instanceof DrawerLayout){
        System.out.println("drawer");
    }

}

}


回答1:


hope this helps you out:)

public void hideSoftKeyboard(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager)activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }



回答2:


Hope the following method will help

searchView.setFocusableInTouchMode(true);



回答3:


Not sure if this will work for action bar but worth a shot. Add this in manifest on the correct activity : android:windowSoftInputMode="stateHidden"




回答4:


In you onCreateOptionsMenu(Menu menu) add this code,this will unable the keyboard issue

mSearchView.setQuery("", false);

mSearchView.setIconified(true); mSearchView.clearFocus();




回答5:


  <EditText

            <requestFocus /><--Remove this from layout>
  </EditText>


来源:https://stackoverflow.com/questions/20987549/key-board-appears-every-time-when-it-open-the-home-activity-of-my-app-unable-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!