开发者

How to show EditText upon Spinner selection?

I have a preliminary question I want users to answer before a certain EditText field should appear. The Spinner is basically a Yes/No question, upon selection Yes I want EditTextA to show, upon selection No I want Edit开发者_如何学PythonTextB to show.

I believe the best way to do this is to have both EditTextA & B hidden by default, appearing upon selection of the Spinner value.

If you could provide step-by-step code, great, but what I am really looking for is high-level direction of how I should approach this.


There is tutorial for you:

http://developer.android.com/resources/tutorials/views/hello-spinner.html

in onItemSelected method add lines:

if(pos == positionOfYes)
    editTextA.setVisibility(View.VISIBLE);
    editTextB.setVisibility(View.GONE); // or INVISIBLE
}else{
    editTextA.setVisibility(View.GONE); // or INVISIBLE
    editTextB.setVisibility(View.VISIBLE);
}


When a user selects a value from your Spinner, it will send an event to the Spinner which you can listen for. To do this, you would set an OnItemSelectedListener on your spinner, which would from there display the proper EditText, or rather, populate your EditText with the correct response.

The method you want to call on your Spinner is setOnItemSelectedListener(AdapterView.OnItemSelectedListener)

The parameter you pass into it is going to specify what it is that you want to do when your Spinner has an item selected. The call will look something like this:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  editText.setText("blah blah blah");
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜