我有两个微调器和一个按钮。第二个微调器的值会根据第一个微调器中选择的项目而改变。
现在,我想要做的是在按下“确定”按钮后根据第二个微调器中的选择启动一个或另一个活动。
以下是主要活动的代码:
public class UniversityActivity extends Activity implements OnItemSelectedListener {
Spinner s1, s2;
Button acceptbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
getActionBar().setDisplayHomeAsUpEnabled(true);
super.onCreate(savedInstanceState);
setContentView(R.layout.university_layout);
addListenerOnButton();
s1 = (Spinner) findViewById(R.id.spinner1);
s2 = (Spinner) findViewById(R.id.spinner2);
s1.setOnItemSelectedListener(this);
}
public void addListenerOnButton() {
final Context context = this;
acceptbutton = (Button) findViewById(R.id.accept);
acceptbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, DegreeActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
});
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String sp1= String.valueOf(s1.getSelectedItem());
if(sp1.contentEquals("Universidad Politécnica de Madrid")) {
List<String> list = new ArrayList<String>();
list.add("Grado en Ingeniería Aeronáutica");
list.add("Grado en Ingeniería Geológica");
list.add("Grado en Ingeniería Informática");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
}
if(sp1.contentEquals("Universidad Autónoma de Madrid")) {
List<String> list = new ArrayList<String>();
list.add("Grado en Historia");
list.add("Grado en Ingeniería Química");
list.add("Grado en Ingeniería en Tecnologías Industriales");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
这就是它的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Select university -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="@string/selectuniv"
android:textColor="@color/yellow" />
<!-- Spinner 1 Dropdown -->
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="5dp"
android:entries="@array/univ" />
<!-- Select degree -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:text="@string/selectdeg"
android:textColor="@color/yellow" />
<!-- Spinner 2 Dropdown -->
<Spinner
android:id="@+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp" />
<!-- Accept button -->
<Button
android:id="@+id/accept"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="13sp"
android:textColor="#ffffff"
android:text="@string/accept"
android:background="@drawable/custombutton2" />
</LinearLayout>
我希望你能帮助我。非常感谢你。
答案 0 :(得分:0)
尝试从Spinner获取所选文字并使用切换或 if-else 。使用getSelectedItem().toString()
例如:
String textFromSpinner = spinner.getSelectedItem().toString();
if(textFromSpinner.equals("TEXT1")){
Intent intent = new Intent(Acivity.this, NextAcivitiy.class);
startActivity(intent);
}
else if(textFromSpinner.equals("TEXT2")){
//start another
}
else if(...){
//similarly
}
答案 1 :(得分:0)
您可以使用setOnItemSelectedListener
loginSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
by String
String selectedItem = parent.getItemAtPosition(position).toString();
if(selectedItem.equalsIgnoreCase("compareString"))
{
Intent intent = new Intent(LoginActivity.this,ONEActivity.class);
startActivity(intent);
}else{
Intent intent = new Intent(LoginActivity.this,TWOActivity.class);
startActivity(intent);
}
按位置
if(position == 1)
Intent intent = new Intent(LoginActivity.this,ONEActivity.class);
startActivity(intent);
}else{
Intent intent = new Intent(LoginActivity.this,TWOActivity.class);
startActivity(intent);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
答案 2 :(得分:0)
试试这个:
String chosenValue = sp2.getSelectedItem().toString();
if(chosenValue.equals("Grado en Historia"){
Intent intent = new Intent(this, AnotherClass.class);
startActivity(intent);
}
else if...
您可以与不同的值进行比较
答案 3 :(得分:0)
从spinner获取文本:
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
检查条件,点击接受按钮,如:
acceptbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(text.equals("selectedItem")){
// do some thing
Intent intent = new Intent(context, DegreeActivity.class);
startActivity(intent);
}
else{
// do some thing
Intent intent = new Intent(context, NextActivity.class);
startActivity(intent);
}
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
});
}
答案 4 :(得分:0)
每当第二个微调器的值发生变化时,通过在文件顶部声明一个字符串将其保存在全局字符串变量中。像这样:
public class UniversityActivity extends Activity implements OnItemSelectedListener {
Spinner s1, s2;
Button acceptbutton;
String _globalString
@Override
protected void onCreate(Bundle savedInstanceState) {
getActionBar().setDisplayHomeAsUpEnabled(true);
super.onCreate(savedInstanceState);
............................................................
现在,每次微调器更改值时,您都需要将所选字段的值保存到该字符串中。
loginSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
_globalString = parent.getItemAtPosition(pos);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
然后您可以访问第二个微调器的最后一个选定值。然后每次按下按钮,你检查我的_globalString匹配的值是多少?
acceptbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(_globalString == "activity1")
{
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
if(_globalString == "activity2")
{
Intent intent = new Intent(context, Activity2.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
}
});
希望这有帮助
答案 5 :(得分:0)
创建一个私有方法,根据微调器选择创建适当的意图。
替换:
Intent intent = new Intent(context, DegreeActivity.class);
与
Intent intent = createActivityIntent(spinner.getSelectedItemPosition());
private Intent createActivityIntent(int position) {
switch(position) {
case 1 : return new Intent(LoginActivity.this, HomeActivity.class);
case 2 : return new Intent(LoginActivity.this, AnotherActivity.class);
default : return new Intent(LoginActivity.this, HomeActivity.class);
}