如何在下拉菜单点击时打开新的活动。

huangapple go评论87阅读模式
英文:

how to open a new activity on spinner dropdown click

问题

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner = findViewById(R.id.spinner);
        flags = findViewById(R.id.flag);

        spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
                CountryData.countryNames));
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                flags.setImageResource(CountryData.countryflag[spinner.getSelectedItemPosition()]);
            }

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

            }
        });
英文:

If someone could help thx.
I have created the spinner part but now whenever I click on the dropdown I need a new activity to open. I was confused where I need to put the if else/ switch case in the Spinner.

@Override 
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    spinner = findViewById(R.id.spinner);
    flags = findViewById(R.id.flag);

    spinner.setAdapter(new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_dropdown_item,
            CountryData.countryNames));
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
            flags.setImageResource(CountryData.countryflag[spinner.getSelectedItemPosition()]);
        }

        @Override
        public void onNothingSelected(AdapterView&lt;?&gt; adapterView) {

        }
    });

答案1

得分: 1

我认为你可以这样做
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity
{
    Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<String> activityList = new ArrayList<>(); // list of string which will be added to Spinner
        activityList.add("选择活动");
        activityList.add("活动 1");
        activityList.add("活动 2");

        spinner = findViewById(R.id.spinner);

        spinner.setAdapter(new ArrayAdapter<>(this,
                                              android.R.layout.simple_spinner_dropdown_item,
                                              activityList
        ));

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
            {
                Log.i("MyTag", "选择的项目在位置 " + i);
                if (i==0) // 检查所选项目的位置并打开活动
                {
                    // 什么都不做
                }
                else if (i==1)
                {
                    openActivity(NewActivity.class);
                }
                else
                {
                    openActivity(NewActivitySecond.class);
                }
            }

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

            }
        });
    }

    private void openActivity(Class x) // 打开活动的函数
    {
        Intent intent = new Intent(this, x);
        startActivity(intent);
    }
}
英文:

I think You can do it like this:

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
{
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList&lt;String&gt; activityList = new ArrayList&lt;&gt;(); // list of string which will be added to Spinner
activityList.add(&quot;Select Activity&quot;);
activityList.add(&quot;Activity 1&quot;);
activityList.add(&quot;Activity 2&quot;);
spinner = findViewById(R.id.spinner);
spinner.setAdapter(new ArrayAdapter&lt;&gt;(this,
android.R.layout.simple_spinner_dropdown_item,
activityList
));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView&lt;?&gt; adapterView, View view, int i, long l)
{
Log.i(&quot;MyTag&quot;, &quot;Item selected at position &quot; + i);
if (i==0) // check position of selected item and open activity
{
// do nothing
}
else if (i==1)
{
openActivity(NewActivity.class);
}
else
{
openActivity(NewActivitySecond.class);
}
}
@Override
public void onNothingSelected(AdapterView&lt;?&gt; adapterView)
{
}
});
}
private void openActivity(Class x) //funtion which opens activity
{
Intent intent = new Intent(this, x);
startActivity(intent);
}
}

huangapple
  • 本文由 发表于 2020年9月7日 05:04:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63768889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定