将微调器链接到 android studio 中的不同单选按钮

Link spinner to different radio buttons in android studio

提问人:Jane 提问时间:11/14/2023 最后编辑:Jane 更新时间:11/14/2023 访问量:10

问:

这是我的主活动中的代码片段。

RadioGroup radioGroup;
    TextView TxtView;
    Spinner spinner;
    TextView result;
    ArrayList<String> countries;
    ArrayAdapter<String> adapter;


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

        countries = new ArrayList<>();
        countries.add("Select");
        countries.add("Spain");
        countries.add("Italy");
        countries.add("Rome");
        countries.add("UK");
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, countries);
        adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);

        radioGroup = findViewById(R.id.radioGroup);
        result = findViewById(R.id.result);
        spinner = findViewById(R.id.spinner);

        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position != 0)
                    result.setText("You have selected " + parent.getSelectedItemId());
            }

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

            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

            

if (checkedId == R.id.radioButton)
                Toast.makeText(getApplicationContext(), "You clicked Button 1", Toast.LENGTH_LONG).show();
            else if (checkedId == R.id.radioButton2)
                Toast.makeText(getApplicationContext(), "You clicked Button 2", Toast.LENGTH_LONG).show();
    });

    }

}

我想找到一种方法将微调器链接到不同的单选按钮。因此,每个单选按钮都会更改微调器中显示的选项集。谁能帮忙写代码?

Android 单选按钮 微调器

评论


答: 暂无答案