当Edittext通过禁用用户键盘获得焦点时,如何打开/填充警告对话框

时间:2015-04-27 10:01:29

标签: android

我创建了一个警告框,用于从位置列表中选择位置。以下是代码

location_ele.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {

               AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
               builder.setTitle("Select Location");
               CharSequence[] cs = locations.toArray(new CharSequence[locations.size()]);
               builder.setItems(cs, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       // the user clicked on colors[which]

                       location_ele.setText(locations.get(which));
                       location = locationsId.get(which).toString();
                   }
               });
                builder.show();
           }
       });

观察我使用onClick Listener实现此代码的代码,它仅适用于onClick,但也需要onFocus。我怎么能这样做?

Screen

您可以看到屏幕截图Cursor正在闪烁,AlertDialog已填充,用户键盘也已填充。我只需要弹出窗口来选择限制用户手动输入文本。 datepicker也发生了一些事情。

2 个答案:

答案 0 :(得分:1)

由于您没有从键盘输入任何输入,我要查找的小部件是Spinner,而不是EditText

答案 1 :(得分:0)

它会帮助你。

        editText.setClickable(true);
        editText.setKeyListener(null);
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"text",Toast.LENGTH_SHORT).show();
                final CharSequence[] items = {
                        "Rajesh", "Mahesh", "Vijayakumar"
                };

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                //builder.setTitle("Make your selection");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        Toast.makeText(MainActivity.this,"text1",Toast.LENGTH_SHORT).show();

                    }
                });
                AlertDialog alert = builder.create();
                alert.show();

            }
        });