Enable or disable an edittext with the selection of a spinner

2

The problem is that it disables text editing but in case 1 of Switch when you have to activate it again it does not

This is the code I'm using.

public class ejemolo extends AppCompatActivity {

    String[] Items = {
            "Dc amps a Kw",
            "Ac una fase amp a kw ",
            "Ac trifasica amps a kw (linia a linea de voltaje)",
            "Ac trifasica amps a kw (linia a voltaje neutral)",


    };

    Spinner s1;

    private String[] listOfObjects;


    EditText ampEditText , voltageEditText , powerfactorEditText  ;

    TextView text1 , text2 , text3, text4 ;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ejemolo);




        FloatingActionButton buttonback = (FloatingActionButton)findViewById(R.id.floatingActionButtonback);
        buttonback.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                Intent intent  = new Intent(v.getContext() , Weight.class);
                startActivityForResult(intent ,0);
            }
        });


        FloatingActionButton buttonhome = (FloatingActionButton)findViewById(R.id.floatingActionButtonhome);
        buttonhome.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                Intent intent  = new Intent(v.getContext() , MainActivity.class);
                startActivityForResult(intent ,0);
            }
        });



        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


        s1 = (Spinner) findViewById(R.id.spinnerAmp);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, Items);

        s1.setAdapter(adapter);


        ampEditText = (EditText)findViewById(R.id.ampEditText);
        voltageEditText = (EditText)findViewById(R.id.voltageEditText);



        powerfactorEditText = (EditText)findViewById(R.id.powerfactorEditText);



        //text1=(TextView)findViewById(R.id.tonsTextResult1);
        //text2=(TextView)findViewById(R.id.tonsTextResult2);


        listOfObjects = getResources().getStringArray(R.array.object_array4);

//        final Spinner spinner = (Spinner)findViewById(R.id.spinnerAmp);


        final android.icu.text.DecimalFormat decimals = new android.icu.text.DecimalFormat("0.00"); /** la cantidad de digitos decimales que se muestra */


       // ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, listOfObjects);




        s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                switch (position) {


                    case 0 :

                        int indzex = s1.getSelectedItemPosition();
                        powerfactorEditText.setFocusable(false);
                        powerfactorEditText.setEnabled(false);
                        powerfactorEditText.setCursorVisible(false);
                        powerfactorEditText.setKeyListener(null);
                        powerfactorEditText.setBackgroundColor(Color.TRANSPARENT);


                        break;



                    case 1:

                        int index = s1.getSelectedItemPosition();
                        powerfactorEditText.setEnabled(true);
                        powerfactorEditText.setInputType(InputType.TYPE_CLASS_TEXT);
                        powerfactorEditText.setFocusable(true);
                        powerfactorEditText.setCursorVisible(true);

                        break;

                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }



}
    
asked by Liantony Pozo 11.05.2017 в 20:15
source

2 answers

2

In the method in which valid if it must be disabled, insert this code fragment:

editText.setFocusable(false);
editText.setEnabled(false);
editText.setCursorVisible(false);
editText.setKeyListener(null);
editText.setBackgroundColor(Color.TRANSPARENT);

I hope it's helpful Greetings.

    
answered by 11.05.2017 в 20:18
1

The error presented is a nullpointerexception (I recommend you always add text, not images)

and it happens here:

 powerfactorEditText.setFocusable(false);

ensure that the reference is in the layout that you load through setContentView(R.layout.activity_ejemolo) , which is activity_ejemolo.xml , look for that there is the EditText with id anguloReferenciaEditText actually.

After solving that you can disable / enable the focus on EditText as you are doing:

powerfactorEditText.setFocusable(false); //true lo enfocas.

or disable the control

powerfactorEditText.setEnabled(true); //false lo deshabilitas.
    
answered by 11.05.2017 в 21:54