问题
I am trying to show an EditText when a certain Item from a Spinner is selected. So far I have created the spinner and the EditText but I don't really know what my next step is. I don't want to display the selection in the EditText, I just want to display the EditText field.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = (EditText) findViewById(R.id.edit_text_box);
editText.getText().toString();
}
public void addItemsOnSpinner()
{
spinner1 = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
(this, R.array.spinner_item, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter (adapter);
}
public void onItemSelected(AdapterView<?> spinner1, View view,int pos, long id)
{
editText.setText(spinner1.getSelectedItem());
}
This is also what I have in my string array + wish to only display the edittext box when item2 is selected
<string-array name="spinner_item">
<item>Item 1</item>
<item value="Item2">Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
</string-array>
回答1:
You should set visibility of editText to invisible or gone in XML, depands on what suits your needs better. then you can use following to make it appear :
public void onItemSelected(AdapterView<?> spinner1, View view,int pos, long id)
{
yourEditText.setVisibility(View.VISIBLE);
}
回答2:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int x = spinner1.getSelectedItemPosition();
valSel = items[x];
}
String spinner1val = spin1.getText().toString(); //extract value of text from spinner
EditText text = (EditText) findViewById(R.id.EditText1); //get id of EditText box
text.setText(spinner1val);
Alternatively, if you want to check the value of item selected in Spinner, the 'valsel' contains the value. Just Toast the valsel.
回答3:
Get the selected values from the spinner,
String anyvariable=String.valueOf(spin.getSelectedItem());
Now you can show this String value in the edit text,
EditText text = (EditText) findViewById(R.id.your_text);
text.setText(anyvariable);
回答4:
@Override
public void onItemSelected(AdapterView<?> month, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
selectedMonth= month.getItemAtPosition(arg2).toString();
Log.d("Tag",""+selectedMonth);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
//selected_item= arg0.getChildAt(1).toString();
}
});
selected_item contains the item ,so just set it to editView like
editView.setText(selected_item)
回答5:
You have to set text on edittext onItemSelected
. First do change as per MKJParekh Suggest.
public void onItemSelected(AdapterView<?> spinner1, View view,int pos, long id)
{
yourEditText.setText(spinner1.getSelectedItem())
}
回答6:
EditText height1, weight1, height, weight; Spinner height_spinner, weight_spinner; String heightInputString, weightInputString; Button calculatebmi;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bmi);
// set your class members as they start out null. // do this for all of them height1 = (EditText) findViewById(R.id.idofheight1inxml); height_spinner = (Spinner) findViewById(R.id.idofheightspinnerinxml); .... // Show the Up button in the action bar. setupActionBar(); setupSpinners(); }
void setupSpinners(){ height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { //I.E. if in the height spinner CM is selected I would like to hide the second height edittext field. // I'm not sure if this is meant to be "height1" or "height" if (position == 0){ height.setVisibility(View.GONE); } else { height.setVisibility(View.VISIBLE); } }
@Override public void onNothingSelected(AdapterView<?> parent) { } }); // if you want to add similar logic for weight spinner, do that with this : weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // put your code here for weight spinner } @Override public void onNothingSelected(AdapterView<?> parent) { } }); }
来源:https://stackoverflow.com/questions/12317093/select-item-from-spinner-to-show-edittext