Edittext无法在加载时隐藏键盘在android中

时间:2017-04-29 12:58:13

标签: android android-activity android-edittext

我在oncreate()方法中有搜索的edittext,但是每当我默认启动活动时,edittext都有RequestFocus()表示它会打开键盘启动活动。我想隐藏那个键盘。以下是我的代码:

public class TwoList extends AppCompatActivity {
    private final String TAG = this.getClass().getName();
    TextView tablenoset, orderidset;
    String OrderIdFromService, Kotidfromservice, item, str, wId, tid, tabletype, newmenuname, fromservertable, serverorderid, response, forcheck, newmenuoflist, uname, tabletid, converttostringtbid, comtid;
    int myvalue, convertedorderid, convertednewtid, convertedorderidforlist, quantityoflist, rateforlist, totalamountoflist, midforlist, tab_id;
    ListView leftList;
    String cntforb;
    ListView rightList;
    ArrayAdapter<String> adapter;
    Context context;

    ArrayList<String> data;
    ArrayAdapter<String> adapter1;
    ArrayList<String> data1;
    Button GetOrder, place_orderbutton;
    private DbHelper mHelper;
    private SQLiteDatabase dataBase;
    private DataForTempNew mydatabase;
    private DatabaseForOrderlist databaseforrate;
    private DataBaseHandlerForMenu menudatafor;
    AlertDialog.Builder alertDialog;
    EditText searchmenu,searchCategory;
    int _id;

    int value;
    String convertedvalue;
    HashMap<Integer, Integer> hasmap = new HashMap<Integer, Integer>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.row);
        Toolbar toolbar = (Toolbar) findViewById(R.id.product_toolBar_title);
        setSupportActionBar(toolbar);
        //place_orderbutton=(Button)findViewById(R.id.button_6);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(TwoList.this, MainActivity.class);
                i.putExtra("Uname", uname);
                i.putExtra("ForButton", cntforb);
                startActivity(i);
            }
        });


        cntforb = TwoList.this.getIntent().getExtras().getString("ForButton");
        uname = TwoList.this.getIntent().getExtras().getString("User_name");
        //tab_id = TwoList.this.getIntent().getExtras().getInt("Tabid");
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        tabletid = prefs.getString("tabid", "no id");
        searchCategory=(EditText)findViewById(R.id.inputSearchcategory);

        alertDialog = new AlertDialog.Builder(TwoList.this);

        GetOrder = (Button) findViewById(R.id.button_5);
        str = TwoList.this.getIntent().getExtras().getString("Mystring");
        wId = TwoList.this.getIntent().getExtras().getString("WaiteriD");
        tid = TwoList.this.getIntent().getExtras().getString("Tid");
        serverorderid = TwoList.this.getIntent().getExtras().getString("OrderIdnew");
        response = TwoList.this.getIntent().getExtras().getString("Yes");
        forcheck = TwoList.this.getIntent().getExtras().getString("No");
        menudatafor = new DataBaseHandlerForMenu(this);
        data = new ArrayList<String>();
        data1 = new ArrayList<String>();
        databaseforrate = new DatabaseForOrderlist(this);
        mydatabase = new DataForTempNew(this);
        mHelper = new DbHelper(this);

        //OrderIdFromService = String.valueOf(tab_id);
        new AsyncLoadOrderId().execute();
        try {
            myvalue = Integer.parseInt(str);
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }
        //myvalue = Integer.parseInt(str);
        ForGettingRate insertformenurate = new ForGettingRate(str);
        new AsyncMenuRate().execute(insertformenurate);
        GetOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TwoList.this, DisplayActivity.class);
                intent.putExtra("Tableno", str);
                intent.putExtra("OrderIds", OrderIdFromService);
                intent.putExtra("WaiterIDs", wId);
                intent.putExtra("Tabletype", tabletype);
                intent.putExtra("user_name", uname);
                intent.putExtra("ForButton", cntforb);
                intent.putExtra("KOTid", Kotidfromservice);
                startActivity(intent);

            }
        });
        if (forcheck != null && forcheck.equals("Yes")) {
            GetOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intentifyes = new Intent(TwoList.this, DisplayActivity.class);
                    intentifyes.putExtra("Tableno", str);
                    intentifyes.putExtra("OrderIds", fromservertable);
                    intentifyes.putExtra("WaiterIDs", wId);
                    intentifyes.putExtra("Tabletype", tabletype);
                    intentifyes.putExtra("KOTid", Kotidfromservice);
                    intentifyes.putExtra("user_name", uname);
                    intentifyes.putExtra("ForButton", cntforb);
                    intentifyes.putExtra("Response", "Yes");
                    startActivity(intentifyes);

                }
            });

        }
        tablenoset = (TextView) findViewById(R.id.tableno_ofneworder);
        orderidset = (TextView) findViewById(R.id.tableorder_id);
        leftList = (ListView) findViewById(R.id.lstLeft);
       /* Typeface custom_font = Typeface.createFromAsset(getAssets(),  "Fonts/DroidSerif-Regular.ttf");
        leftli.setTypeface(custom_font);*/
        //tablenoset.setText(str);
        getTableno();
        context = this;

        searchCategory.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) {
                TwoList.this.adapter.getFilter().filter(s);

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        leftList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedFromList = (leftList.getItemAtPosition(position).toString());

                InsertCategoryFrom insertfromhere = new InsertCategoryFrom(selectedFromList);
                new AsyncCreateCategory().execute(insertfromhere);
                LayoutInflater inflater = getLayoutInflater();
                View convertView = (View) inflater.inflate(R.layout.nnsample, null);
                alertDialog.setTitle("Menu");
                rightList = (ListView) convertView.findViewById(R.id.lstRight);
                searchmenu = (EditText) convertView.findViewById(R.id.inputSearch);
                searchmenu.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) {
                        TwoList.this.adapter1.getFilter().filter(s);
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                    }
                });
                populateRightList();
                //alertDialog.setView(searchmenu);
                alertDialog.setView(convertView);
                adapter1.clear();
                adapter1.notifyDataSetChanged();
                alertDialog.show();


            }
        });
        alertDialog.setPositiveButton("Back", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                AlertDialog alert = alertDialog.create();
                alert.dismiss();
            }
        });


        populateLeftList();


    }
    public void hideSoftKeyboard() {
        if(getCurrentFocus()!=null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    }

3 个答案:

答案 0 :(得分:2)

你需要设置 manifeststs.xml中的android:windowSoftInputMode="stateHidden”

或尝试

android:windowSoftInputMode="adjustPan"

您还可以参考以下链接:

https://developer.android.com/training/keyboard-input/visibility.html#ShowOnStart

答案 1 :(得分:2)

要隐藏键盘时使用此代码

View v = getCurrentFocus();
                if (v != null) {
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }

答案 2 :(得分:0)

在我的父版面上,添加

 android:focusable="true"
 android:focusableInTouchMode="true"

示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:id="@+id/et1"/>

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

    </ListView>

</LinearLayout>