焦点文本字段

时间:2019-02-20 11:05:44

标签: android

我有几个“ TextInputEditText”,其中一些具有这种行为

etField.setOnClickListener(this);
etField.setFocusable(false);

根据情况,如果用户单击编辑文本,则显示带有选择项的“ DatePickerDialog”或“ AlertDialog”。

我的键盘有问题。 用户在第一个普通编辑文本中输入信息,如果用户单击“下一步”,则跳过下一个文本(具有以前的行为),直到下一个普通文本为止。

如果我没有焦点,如何给焦点显示相同的alertDialog或DatePickerDialog?

我的密码部分

if (type.equalsIgnoreCase(TypeField.DATE)) {
            etField.setOnClickListener(this);
            etField.setFocusable(false);
            etField.setInputType(InputType.TYPE_CLASS_DATETIME);
            etField.setText(value);



        } else if (type.equalsIgnoreCase(TypeField.CHAINE)) {
            etField.setInputType(InputType.TYPE_CLASS_TEXT);
            etField.setText(value);
            if (nom != null && nom.size() > 0)
            {

                etField.setFocusable(false);
                etField.setOnClickListener(this);




            }

        } else if (type.equalsIgnoreCase(TypeField.ENTIER)) {
            etField.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
            etField.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
            etField.setText(value);

        } else if (type.equalsIgnoreCase(TypeField.REEL)) {

            etField.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

}

@Override public void onClick(View v){

        if (type.equalsIgnoreCase(TypeField.DATE)){
            Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
            DatePickerDialog dialog = new DatePickerDialog(getContext(), this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));


            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {

                }
            });

            dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Annuler", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {

                    }
                }
            });
            dialog.show();

        }
        else if (type.equalsIgnoreCase(TypeField.CHAINE)){
            String valueSelected = this.getValue();

            String[] labels = [...]

            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setSingleChoiceItems(labels, iChecked, new ItemSelectionChanged());

            AlertDialog alertDialog = builder.create();

            alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
                @Override
                public void onCancel(DialogInterface dialogInterface) {

                }
            });

            String titleChoice = fieldDescription.getLabel();
            alertDialog.setTitle(titleChoice);
            //alertDialog.setButton(Dialog.BUTTON_POSITIVE,"OK", new ItemActionChanged());
            //alertDialog.setButton(Dialog.BUTTON_NEGATIVE,"ANNULER", new ItemActionChanged());
            //alertDialog.setButton(Dialog.BUTTON_NEUTRAL,"EFFACER", new ItemActionChanged());
            alertDialog.show();
        }
}

3 个答案:

答案 0 :(得分:0)

使用文本观察器我们可以实现。

et1.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {


//DatePickerDialog  code comes here


        // TODO Auto-generated method stub
    }
});

答案 1 :(得分:0)

解决方案: 在XML中,我这样做。

            <EditText
                    android:clickable="false"
                    android:focusable="false"
                    android:id="@+id/edt_treeCondition"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

对于我的根标签,我将其设为可聚焦真,因为布局需要聚焦。

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:background="@color/colorWhite"
        android:layout_height="match_parent">

现在,成功使用点击侦听器。

edt_treeCondition.setOnClickListener{
//Do whatever you want
}

我实际上并不完全理解这个问题,但是希望这对您的用例有所帮助。

答案 2 :(得分:0)

如果可以为某人服务,对我来说解决方案是:

etField.setOnClickListener(this);
//etField.setFocusable(false);
//etField.setFocusableInTouchMode(false);
etField.setInputType(InputType.TYPE_CLASS_DATETIME);
etField.setText(value);

etField.setFocusable(true);
etField.setFocusableInTouchMode(true);
etField.setTextIsSelectable(false);
etField.setShowSoftInputOnFocus(false);
activeFocus(etField,tilField);

 private void activeFocus(EditText etField, TextInputLayout tilField) {

        etField.setShowSoftInputOnFocus(false);
        etField.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                GILogUtil.i(TAG, "focus");
                if (hasFocus == true){
                    onClick(etField);
                }
            }
        });


    }