我想使用微调器视图添加两个数字。在我的代码中有两个微调器。在我运行模拟器后,它只显示直接结果。它不显示微调控件,我无法选择这两个数字。请给出一个解决方案。提前致谢。代码
package com.kk;
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.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.R.id;
public class TrckActivity extends Activity {
/** Called when the activity is first created. */
String[] a={"-select-","1","2"};
String[] b={"-select-","2","4"};
int first,second,f,s,c;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> a1= new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,a);
final Spinner sp1=(Spinner)findViewById(R.id.spinner1);
sp1.setAdapter(a1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
first=sp1.getSelectedItemPosition();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter<String> a2= new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,b);
final Spinner sp2=(Spinner)findViewById(R.id.spinner1);
sp2.setAdapter(a2);
sp2.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
second=sp2.getSelectedItemPosition();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
if(first==1)
{
f=1;
}
else if(first==2)
{
f=2;
}
if(second==1)
{
s=2;
}
else if(second==2)
{
s=3;
}
c=f+s;
TextView tv=new TextView(this);
tv.setText(""+c);
setContentView(tv);
}
}
答案 0 :(得分:0)
这可能是因为一旦您的代码进入onCreate方法,最初就会调用微调器的“onItemSelected”方法。也许你必须保持标志值才能做到这一点。
这些链接可能会帮助您开始使用它,
Spinner onItemSelected called erroneously (without user action)
答案 1 :(得分:0)
尝试交换
android.R.layout.simple_dropdown_item_1line
到
android.R.layout.simple_spinner_item