简体   繁体   English

出现键盘时Edittext不会获得焦点

[英]Edittext gets no focus when keyboard appears

A have a few problems with the UI. 用户界面存在一些问题。 I have a listview with static items. 我有一个静态项目的列表视图。 Inside of them there are some edittexts. 它们内部有一些edittext。 However, when I click on any editText it gets unfocused, the keyboard appears and I need to click on it again to type something! 但是,当我单击任何editText时,它都没有对准焦点,键盘就会出现,我需要再次单击它以键入内容! The number edittext gets no focus at all. 数字edittext完全没有焦点。 To make matters worse, a button below the listview behaves like floating action button and appears above the keybord. 更糟糕的是,列表视图下方的按钮的行为类似于浮动操作按钮,并出现在键盘上方。 How can I fix this? 我怎样才能解决这个问题?

Here are some screens and code samples. 这是一些屏幕和代码示例。

MainActivity.class MainActivity.class

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
     EditText text;
        private ListViewAddAdapter listViewAddAdapter;
        private ListView listView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);




            ArrayList<ListViewItem> listViewItems=new ArrayList<>();
            listViewItems.add(new ListViewItem(0));
            listViewItems.add(new ListViewItem(1));
            listViewItems.add(new ListViewItem(2));

            listView= (ListView) findViewById(R.id.listView);

            listViewAddAdapter=new ListViewAddAdapter(this,listViewItems);
            listView.setAdapter(listViewAddAdapter);
            //listViewAddAdapter.notifyDataSetChanged();
            text= (EditText) findViewById(R.id.editText);

            Button b= (Button) findViewById(R.id.button);
            b.setOnClickListener(this);




        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return 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);
        }

        @Override
        public void onClick(View view) {


            Log.d("Mlog",listViewAddAdapter.name);





        }
    }

ListViwItem.java ListViwItem.java

public class ListViewItem
{
   public int type;

    public ListViewItem(int type) {
        this.type = type;
    }
}

ListViewAdd.java ListViewAdd.java

public class ListViewAddAdapter extends BaseAdapter
{
    Context ctx;
    LayoutInflater inflater;
    ArrayList<Person> persons;
    String name;
    int TYPE0=0;
    int TYPE_PHONE=1;
    int TYPE_ADDRESS=2;
    ArrayList<ListViewItem> listViewItems;

    @Override
    public int getItemViewType(int position) {
        return listViewItems.get(position).type;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }


public String getName()
{
    return this.name;
}

    @Override
    public int getCount() {
        return listViewItems.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public ListViewAddAdapter(Context context, ArrayList<ListViewItem> listViewItems) {
        ctx=context;
        inflater= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.listViewItems=listViewItems;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View view1=view;
        if(view1==null)
        {
            if (this.getItemViewType(i)==TYPE0) {
                view1 = inflater.inflate(R.layout.add_item0, viewGroup, false);

                final EditText editText= (EditText) view1.findViewById(R.id.editText);
                editText.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        name=editText.getText().toString();

                        Log.d("MMM",name);
                    }
                });

            }
            else if (this.getItemViewType(i)==TYPE_PHONE)
                view1=inflater.inflate(R.layout.list_tem_ph_numb,viewGroup,false);
            else if (this.getItemViewType(i)==TYPE_ADDRESS)
                view1=inflater.inflate(R.layout.list_item_address,viewGroup,false);


        }


        return view1;
    }
}

So here's how it looks Initial look 所以这是它的外观初始外观

After EditTextNAme was pressed 按下EditTextNAme之后

单击Edittext后,尝试以下操作:

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

Try this: 尝试这个:

text.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            if(hasFocus){
                imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
            } else {
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
       }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM