我想问一下如何在同一个活动中同时使用微调器和按钮。 Spinner使用onItemListener监听,然后该按钮也会有一个onClick监听器。所以在我的情况下,它会产生错误。我的方案是我从微调器中获取选定的字符串,然后从editTexts获取其余值,然后点击“submit”将数据发送到服务器。但我估计这两位听众对彼此不是很友好?
我为微调器设置了这些方法:
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
}
});
然后我需要为按钮设置onclick监听器:
go.setOnClickListener(new OnClickListener(){
public void onClick(View arg0)
{
}
我在哪里放这个?在nothingSelected方法之前或之后?
答案 0 :(得分:2)
我认为他们彼此非常友好:)
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
}
});
go.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// your code here
}
});
答案 1 :(得分:-1)
public class StackOverflowActivity extends Activity {
private static final String[] SPINNER_DATA = new String[] { "Item 1", "Item 2" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spn = (Spinner) findViewById(R.id.spinner1);
spn.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, SPINNER_DATA));
spn.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"Spinner.onItemSelected()", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"Spinner.onNothingSelected()", Toast.LENGTH_LONG)
.show();
}
});
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Button.onClick()",
Toast.LENGTH_LONG).show();
}
});
}
}