键盘出现时,Edittext无法聚焦

时间:2016-08-06 08:23:53

标签: android

A有一些UI问题。 我有一个静态项目列表视图。 在他们里面有一些edittexts。但是,当我点击任何editText它没有聚焦时,键盘出现,我需要再次点击它键入一些东西! 数字edittext根本没有焦点。 更糟糕的是,listview下方的按钮的行为类似于浮动操作按钮,并显示在键盘上方。我该如何解决这个问题?

以下是一些屏幕和代码示例。

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

public class ListViewItem
{
   public int type;

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

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;
    }
}

所以这就是它的外观 Initial look

After EditTextNAme was pressed

2 个答案:

答案 0 :(得分:1)

点击Edittext后试试这个:

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

答案 1 :(得分:0)

试试这个:

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);
            }
       }
    });