gpt4 book ai didi

android - 一项 Activity 中的两个微调器

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:51 25 4
gpt4 key购买 nike

我有 2 个微调器。

这是我的 Activity

package com.example.belajarcombobox;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class CreateComboBox extends Activity implements AdapterView.OnItemSelectedListener {

TextView selection;
TextView selection2;
String[] countries = {"Amerika","Argentina","Brazil"};

String[] city = {"London","Paris","Buenos Aires"};

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

selection = (TextView) findViewById(R.id.selection);
Spinner spin = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, countries);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
spin.setOnItemSelectedListener(this);

selection2 = (TextView) findViewById(R.id.selection2);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> bb = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, city);
bb.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(bb);
spin2.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> parent, View v, int position, long id ){
Spinner spin = (Spinner)parent;
Spinner spin2 = (Spinner)parent;
if(spin.getId() == R.id.spinner)
{
Toast.makeText(this, "Your choose :" + countries[position],Toast.LENGTH_SHORT).show();
}
if(spin2.getId() == R.id.spinner2)
{
Toast.makeText(this, "Your choose :" + city[position],Toast.LENGTH_SHORT).show();
}

}

public void onNothingSelected(AdapterView<?> parent){
Toast.makeText(this, "Choose Countries :", Toast.LENGTH_SHORT).show();
}
}

当我运行此 Activity 时,第一个微调器值为城市,第二个微调器值为空。

如何在同一 Activity 中运行 2 个微调器?第一个微调器值是国家,第二个微调器值是城市。

BR

亚历克斯

最佳答案

不要将实现添加到您的 Activity 类

创建两个内部类,每个处理一个微调器

public class CreateComboBox extends Activity implements AdapterView.OnItemSelectedListener
{
TextView selection;
TextView selection2;
String[] countries = {"Amerika","Argentina","Brazil"};

String[] city = {"London","Paris","Buenos Aires"};

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

selection = (TextView) findViewById(R.id.selection);
Spinner spin = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, countries);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
spin.setOnItemSelectedListener(new CountriesSpinnerClass());

selection2 = (TextView) findViewById(R.id.selection2);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> bb = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, city);
bb.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(bb);
spin2.setOnItemSelectedListener(new CitiesSpinnerClass());
}

class CountriesSpinnerClass implements AdapterView.OnItemSelectedListener
{
void onItemSelected(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(v.getContext(), "Your choose :" + countries[position],Toast.LENGTH_SHORT).show();
}
}

class CitiesSpinnerClass implements AdapterView.OnItemSelectedListener
{
void onItemSelected(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(v.getContext(), "Your choose :" + city[position],Toast.LENGTH_SHORT).show();
}
}
}

关于android - 一项 Activity 中的两个微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13909109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com