Deleting an editText while modifying another

2

I'm starting my studies on Android now, and I'm doing some testing.

I have two AutoComplitTextView fields, the two shows an id and a description. X1 and X2 (fictitious names). The X2 list is filtered by choosing X1. Is there any way that if the user erases the id (even if only a number) from X1 the field X2 is cleared?

X1 = Cost X2 = Operation

My fields xml is like this.

    <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();
        }
    }
});
    
asked by anonymous 01.07.2016 / 14:43

2 answers

1

Yes, you will need to add TextWatcher to 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 the edit receives the new value, when it receives and after it has received.

In this case you would need to use afterTextChanged , it would be triggered after your edit receives a new value, so you clear x2.

Obs: If you need to assign a value to x1, and you do not want it to delete the value of x2, you need to remove TextWatcher before assigning, and then add again. >     

01.07.2016 / 15:28
1

Speak Edu,

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

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

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

Hugs.

    
01.07.2016 / 15:23