简体   繁体   English

想要添加一个按钮以根据第一个微调器选择更改意图

[英]Want to add a button to change the intent based on the 1st spinner selection

    final Spinner s1= (Spinner) findViewById(R.id.Spinner1);
    final Spinner s2= (Spinner) findViewById(R.id.Spinner2);
    final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Manu_array, R.layout.textview);

    s1.setAdapter(adapter);
    s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            int arrayId = 0;
            switch (position) {//get array id according to selected position
                case 0:
                    arrayId = R.array.Manu_1xsd_array;
                    break;
                case 1:
                    arrayId = R.array.Manu_2xrsd;
                    break;
                case 2:
                    arrayId = R.array.Manu_3x4rsd.array;
                    break;
            }
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, arrayId, R.layout.textview);
            s2.setAdapter(adapter);
        }

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

        }
    });
    s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String msg = s1.getSelectedItem().toString() + s2.getAdapter().getItem(position).toString();
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
        }

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

        }
    });

My button code is as follows:我的按钮代码如下:

Button save_control_button = (Button) findViewById(R.id.save_control_button);
    save_control_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

//some code

            Intent passIntent = new Intent(getApplicationContext(),Manu_1xsd.class);
            startActivity(passIntent);

        }
    });

// Want to change the intent with the spinner 1 selection so when I press the button it takes everything and loads up with slightly different pararmeters which are in the relevant class, just cant figure out how to change the intent based on the spinner selection and cant fins an example, can anyone point me i the right direction? // 想要使用微调器 1 选择更改意图,因此当我按下按钮时,它会获取所有内容并加载相关类中略有不同的参数,只是无法弄清楚如何根据微调器选择更改意图和不能举个例子,有人能指出我正确的方向吗?

What you can do is read the value of the spinner and use it to launch the correct intent.您可以做的是读取微调器的值并使用它来启动正确的意图。

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent;
            switch (spinner.getSelectedItem()) {
                case "Manu1":
                    intent = new Intent(getApplicationContext(), Manu_1xsd.class);
                    break;
                case "Manu2":
                default:
                    intent = new Intent(getApplicationContext(), Manu_2xsd.class);
                    break;
            }

            startActivity(intent);
        }
    });

If you are using an older version of Java where you can't use a switch statement for Strings, you can use AdapterView.getSelectedItemId() instead.如果您使用的是不能为字符串使用 switch 语句的旧版 Java,则可以改用AdapterView.getSelectedItemId()

Note - I haven't actually ran this code but rather just wrote it up.注意 - 我实际上并没有运行这段代码,而只是写下了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM