Delete an editText when modifying another

Asked

Viewed 308 times

2

I’m starting my studies on Android now, and I’m doing some tests.

I have two Autocomplittextview fields, in both an id and a description is shown. X1 and X2 (fictitious names). The X2 list is filtered by the choice of X1. Has Some way that if the user deletes the id(even if it is only a number) of X1 the field X2 is cleared?

X1 = Ccusto X2 = Operation

My fields xml ta so.

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="C.Custo"
        android:id="@+id/textView14"
        android:textSize="25sp" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <AutoCompleteTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/et_ccusto"
            android:layout_weight="1"
            android:textSize="20sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/btn_ccusto"
            android:textSize="20sp" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Operacão"
        android:id="@+id/textView15"
        android:textSize="25sp" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <AutoCompleteTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/et_operacao"
            android:layout_weight="1"
            android:textSize="20sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/btn_operacao"
            android:textSize="20sp" />

I’m using this in my Activity.

 acetCcusto.setAdapter(adapterCcusto);  
 acetOperacao.setOnFocusChangeListener(new View.OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
      if(!acetCcusto.getText().toString().equals("")){
          int cdEquipamento = util.getInt(tvEquip.getText().toString());
          int cdCcusto = util.getInt(acetCcusto.getText().toString());
          adapterOperacao = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, CcBO.getArrayOperacao(cdCcusto));
          adapterOperacao = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, EOBO.getArrayCdOperacao(cdEquipamento));
          acetOperacao.setAdapter(adapterOperacao);

      }
    }
});

btnCcusto.setOnClickListener(new View.Onclicklistener() {

   @Override
    public void onClick(View v) {
        Log.i(TAG, "onClick: ");
        new MaterialDialog.Builder(context)
                .title(R.string.title)
                .items(CcBO.getArrayCcusto())
                .itemsCallback(new MaterialDialog.ListCallback() {
                    @Override
                    public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                        acetCcusto.setText(text.toString());
                        acetOperacao.setText("");
                    }
                })
                .show();
    }
});
btnOperacao.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (!acetCcusto.getText().toString().equals("")) {
            int cdCcusto = util.getInt(acetCcusto.getText().toString());
            cdOperacao = CcBO.getArrayOperacao(cdCcusto);
            if (cdOperacao!=null){
                adapterOperacao = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, cdOperacao);
            }
            new MaterialDialog.Builder(context)
                    .title(R.string.title)
                    .items(CcBO.getArrayOperacao(cdCcusto))
                    .itemsCallback(new MaterialDialog.ListCallback() {
                        @Override
                        public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                            acetOperacao.setText(text.toString());
                        }
                    })
                    .show();
        }
    }
});

2 answers

1


Yes, you will need to add one TextWatcher on your X1.

Example:

edtx1.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) {

                edtx2.setText("");
            }
        });

Through the three methods present in TextWatcher you can monitor before Edit receives the new value, when it receives and after it has received.

In that case you would need to use afterTextChanged, would be triggered after your Edit received a new value, so you clear the x2.

Obs: If you need to assign a value to X1, and you don’t want it to erase the value of x2, you need to remove the TextWatcher before assigning, and then add again.

  • That’s right. Thank you

1

Fala Edu,

You can check if the field is empty, and then delete the second field, example:

if(TextUtils.isEmpty(campoX1)) {
    campoX2.setText("");
    return;
 }

If field 1 is empty, the second will also be.

Hugs.

  • So leo I thought of this ipothesis, but if I put this command it will erase the X2 as soon as you click on X1. I want you to delete X2 only if X1 is deleted.

  • I edited my answer, give a look, so you check if the field is empty

  • Thanks for your help. but Victor already gave me what I was looking for

Browser other questions tagged

You are not signed in. Login or sign up in order to post.