0

I've created a spinner for my application. This is the code in the main.xml

 <Spinner android:id="@+id/sMenu" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:prompt="@string/base" /> 

and in my main class

 String [] base = {"Decimale", "Binaria", "Ottale", "Esadecimale"}; Spinner s1; s1 = (Spinner) findViewById (R.id.sMenu); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.base, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); s1.setAdapter(adapter); 

The program converts a number in binary, decimal, octal, hexadecimal. I want to associated some events to the spinner. For example, if I select in the spinner binary, in the edit text I can put only binary numbers. In which way I can associate this event to the spinner?

1
  • What did you try? Did you even try to find a solution before pasting that code here? Commented Nov 29, 2011 at 22:07

1 Answer 1

1
s1.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View v, int position, long id3) { final String b = adapter.getItem(position); // based on b you change the property inputType of your EditText // for instance, if b is 'deciaml' you set as inputType 'numberDecimal' } @Override public void onNothingSelected(AdapterView<?> parent) { } }); 
Sign up to request clarification or add additional context in comments.

4 Comments

but in which way can indicate the position?
the position is a parameter passed by the framework in the method ... onItemSelected(AdapterView<?> parent, View v, int position, long id3)...
but how can i associate the inputtype binary for example, with the string b?
if ("Decimale".equals(b)). There is no other way unless you compare the position if (position == 0), but in this last case if then you move the set of values to arrays.xml you can't anymore.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.