Home » Android » android – problems with Focus by two edittexts-Exceptionshub

android – problems with Focus by two edittexts-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have two EditText field and I try, when I scann in EditText1 that the focus go to EditText2. But all what happen is that the focus goes a short time to EditText2 and go back to EditText1. But i have no idea why. Can Someone help me or can give me a tip please? My Code is the following:

public class Inventory extends AppCompatActivity implements
AdapterView.OnItemSelectedListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        Edittext1.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int inType = Edittext1.getInputType(); // backup the input type
                Edittext1.setText("");
                Edittext1.setInputType(InputType.TYPE_NULL); // disable soft input
                Edittext1.onTouchEvent(event); // call native handler
                Edittext1.setInputType(inType); // restore input type
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(Edittext1.getWindowToken(), 0);
                return true; // consume touch even
            }
        });

        EditText2.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int inType = EditText2.getInputType(); // backup the input type
                EditText2.setText("");
                EditText2.setInputType(InputType.TYPE_NULL); // disable soft input
                EditText2.onTouchEvent(event); // call native handler
                EditText2.setInputType(inType);
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(EditText2.getWindowToken(), 0);
                return true; // consume touch even
            }
        });

        EditText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    if (!EditText2.getText().toString().isEmpty()){

                        ParameterClass.Artartid = "null";

                        ParameterClass.ArtcileOrEAN = "article";

                        ParameterClass.eanisthere = "";
                        ParameterClass.eanartid = "";
                        ParameterClass.eanAmount = "";
                        ParameterClass.ean = "";

                        getEAN(EditText2.getText().toString());

                        if(ParameterClass.eanartid != null) {

                            getArticleInformation(ParameterClass.eanartid);

                        }

                        if (ParameterClass.Artartid.isEmpty() || ParameterClass.Artartid.equals("null")){

                            EditText2.setText("");

                        } else {

                            artbez.setText(ParameterClass.artname);
                        }

                    }
                }
            }
        });

        EditText2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (v.getId() == R.id.EditText2) {

                    ParameterClass.ArtcileOrEAN = "article";

                    ParameterClass.eanisthere = "";
                    ParameterClass.eanartid = "";
                    ParameterClass.eanAmount = "";
                    ParameterClass.ean = "";

                    getEAN(String.valueOf(EditText2.getText()));

                    if (ParameterClass.eanartid == null || ParameterClass.eanAmount == null ||
                        ParameterClass.eanartid.isEmpty() || ParameterClass.eanAmount.isEmpty()) {

                        ParameterClass.ean = String.valueOf(EditText2.getText());
                        getArticleOnlyWithScanner(ParameterClass.ean);

                        InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                    } else {
                        getArtnName(ParameterClass.eanartid);

                        InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                        amountinventur.setText(ParameterClass.eanAmount);

                        if(ParameterClass.loginAfterScannSave == null || ParameterClass.loginAfterScannSave.equals("false")){
                            SaveInventory();
                        } else {
                            amountinventur.requestFocus();
                        }
                    }
                }
                return false;
            }
        });

        EditText1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                                                          @Override
                                                          public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
              if (v.getId() == R.id.EditText1) {

                  if (!EditText1.getText().toString().isEmpty()) {
                      ParameterClass.StockSearch = "null";
                      ParameterClass.stockID = "null";

                      getStockInformation(EditText1.getText().toString());

                      if (ParameterClass.stockID.isEmpty() || ParameterClass.stockID.equals("null")) {

                          Stockalert();
                          EditText1.setText("");
                      } else {

                          textView3.setText("Lager: " + ParameterClass.stockName);

                          EditText1.clearFocus();
                          EditText1.setCursorVisible(false);
                          EditText2.setCursorVisible(true);
                          EditText2.requestFocus();
                      }
                  }
              }
        return false;
        }
        });


        EditText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    if (!EditText1.getText().toString().isEmpty()) {
                        ParameterClass.StockSearch = "null";
                        ParameterClass.stockID = "null";

                        getStockInformation(EditText1.getText().toString());

                        if (ParameterClass.stockID.isEmpty() || ParameterClass.stockID.equals("null")) {

                            Stockalert();
                            EditText1.setText("");
                        }

                    }
                }
            }
        });
    }
}
How to&Answers: