How do I make so my spinner target links to a webs

2019-02-20 14:45发布

Hey, I'm really new with android development just started a few hours ago. I need to make a application for a school project and it needs to be done soon.

So far have I made a spinner with some alternatives to choose from. But here is my problem. How do I make so when I click on one of the targets listed in my spinner so it links to a webpage.

I don't know how to use "AdapterView.OnItemSelectedListener." or if it is necessary here or not.

I would be very thankful for some advices. thanks a head

1条回答
Emotional °昔
2楼-- · 2019-02-20 15:11

You can try setting the onItemSelectedListener to your spinner:

EDIT:

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.planets_array, android.R.layout.simple_spinner_item);   
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

  @Override
  public void onItemSelected(AdapterView<?> arg0, View arg1,
          int arg2, long arg3) {
      startActivity(new Intent(Intent.ACTION_VIEW,
             Uri.parse("http://yoururl.com")));
  }

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

});

This will open the browser with your url.

查看更多
登录 后发表回答