开发者

Android spinner and button

I wanted to ask how can one use both spinner and button on the same activity. Spinner listens with its onItemListener and then the button would have an onClick listener too. So in my case, it generates an error. My scenario is that I get the selected string from the sp开发者_如何转开发inner and then the rest of the values from editTexts and then hit "submit" to send data to the server. But I reckon these two listeners aren't very friendly with each other?

I set up these methods for the spinner right:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        // your code here
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }
});

Then I need to put the onclick listener for the button:

go.setOnClickListener(new OnClickListener(){
    public void onClick(View arg0)
    {
    }

Where do I put this one? Before the nothingSelected method or after that?


I think they are very friendly with each other :)

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // your code here
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });

    go.setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View arg0) {       
          // your code here                
       }
   });


public class StackOverflowActivity extends Activity {

    private static final String[] SPINNER_DATA = new String[] { "Item 1", "Item 2" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner spn = (Spinner) findViewById(R.id.spinner1);
        spn.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, SPINNER_DATA));
        spn.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(),
                        "Spinner.onItemSelected()", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(),
                        "Spinner.onNothingSelected()", Toast.LENGTH_LONG)
                        .show();
            }
        });

        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Button.onClick()",
                        Toast.LENGTH_LONG).show();
            }
        });

    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜