我在Android中正在做一个项目,我想在同一个活动中添加第二个微调器。它应该独立于第一个微调器。
我该怎么办?
import android.support.v7.app.AppCompatActivity;
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;
import static android.R.layout.simple_dropdown_item_1line;
import static android.R.layout.simple_spinner_item;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter=ArrayAdapter.createFromResource(this, R.array.field_name, android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView myText= (TextView) view;
Toast.makeText(this,"You have Selected "+ myText.getText(),Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
接下来需要做什么?
答案 0 :(得分:2)
创建另一个微调器spinner2
然后重复你的代码:
spinner2=(Spinner) findViewById(R.id.spinner2);
ArrayAdapter adapter2=ArrayAdapter.createFromResource(this, R.array.field_name, android.R.layout.simple_spinner_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(this);
不要忘记在 .xml 中创建另一个微调器,即spinner2。
完整的代码:
public class MainActivity extends AppCompatActivity {
Spinner spinner,spinner2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter=ArrayAdapter.createFromResource(this, R.array.field_name, android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner2=(Spinner) findViewById(R.id.spinner2);
ArrayAdapter adapter2=ArrayAdapter.createFromResource(this, R.array.field_name, android.R.layout.simple_spinner_item);
spinner2.setAdapter(adapter2);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
}
答案 1 :(得分:1)
将另一个Spinner添加到XML中,并以相同的方式声明并初始化它。
Spinner spinner1 , spinner2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
ArrayAdapter adapter1=ArrayAdapter.createFromResource(this, R.array.field_name, android.R.layout.simple_spinner_item);
ArrayAdapter adapter2=ArrayAdapter.createFromResource(this, R.array.field_name, android.R.layout.simple_spinner2_item);
spinner1.setAdapter(adapter);
spinner2.setAdapter(adapter2);
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
}
您不需要为同一布局中的每个微调器重写单独的方法。以下内容适用于spinner1和spinner2。您可以执行检查以了解选择了哪个微调器的项目
@Override
public void onItemSelected(AdapterView<?> parentView, View view, int position, long id) {
Spinner spinner = (Spinner) parentView;
if(spinner.getId() == R.id.spinner1){
//do this
}
else if(spinner.getId() == R.id.spinner2){
//do this
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// your code here
}
请记住,对于一个微调器,OnItemSelectedListener事件将执行两次 -
您可以使用标志变量来区分这两者。