简体   繁体   English

Edittext 必须始终是焦点

[英]Edittext have to be focus always

I have an EditText.我有一个 EditText。 I want this edittext to always be in focus and writeable.我希望此编辑文本始终处于焦点且可写。 How can I do this without touching it every time?我怎样才能做到这一点而不每次都触摸它? Because barcode scanner machine sends data successive to this app.因为条码扫描仪机器向这个应用程序连续发送数据。

anasayfa_barkod.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            if ((keyEvent.getAction() == KeyEvent.ACTION_DOWN) &&
                    (i == KeyEvent.KEYCODE_ENTER)) {
                adapter.clear();
                adapter.notifyDataSetChanged();
                geciciListeyeEkle();
                listeyiYukle();
                adapter.notifyDataSetChanged();
                tutarYazdir();

                if(anasayfa_verilenUcret.getText().length()>0){
                    try{
                        String ucret=anasayfa_verilenUcret.toString();
                        String paraustu=String.valueOf(Double.parseDouble(ucret)-gelenSatis);
                        anasayfa_paraustu.setText(paraustu);
                    }catch (NumberFormatException e){
                        e.printStackTrace();
                    }
                }
                anasayfa_barkod.requestFocus(); //not working
                return true;
            }
            return false;
        }
    });

I tried a lot of method.but all of them is not working.我尝试了很多方法。但它们都不起作用。 I can set the keyboard is visible but cursor not on edittext.我可以设置键盘可见但光标不在编辑文本上。

I solved my problem with enter link description here在这里输入链接描述解决了我的问题

new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if ( anasayfa_barkod!= null) {
                            anasayfa_barkod.requestFocus();
                        }
                    }
                }, 1000);

I think may I can help.我想我可以帮忙。 Use this in onCreate method在 onCreate 方法中使用它

editText.requestFocus();

it works for me.这个对我有用。

If you are in activity add this in your onCreate method:如果您在活动中,请在您的 onCreate 方法中添加:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT‌​_INPUT_STATE_ALWAYS_‌​VISIBLE);

if you are in fragment use this:如果你在片段中使用这个:

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT‌​_INPUT_STATE_ALWAYS_‌​VISIBLE);

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

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