如何使用onItemSelected在Android的? [英] How can I use onItemSelected in Android?
问题描述
package org.example.mbtiapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MBTITest extends Activity implements OnItemSelectedListener {
private Spinner firstSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mbtitest);
Spinner firstSpinner = (Spinner) findViewById(R.id.spinner1);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinnerarraybool, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
firstSpinner.setAdapter(adapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
XML布局:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="I like to go out more than staying home." />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:textSize="10.5dp"
android:text="Sensing v Intuition" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
我是一个新的Android程序员,有麻烦用微调,我已经试过多个教程,仍然感到困惑。我想知道我的下一步是什么在这里,因为据我所知,我已经建立了我的XML微调,在Java中我已经确定了微调,创造了说的微调ArrayAdapter,并指定一些选项。我不知道我是否已经填充了微调又或如何与微调对象manuever。我希望能够使用微调对象选择三个选项之一,然后保持的TextView内该值的微调内部。
I'm a new Android programmer and having trouble with using Spinner, i've tried multiple tutorials and still am confused. I'd like to know what my next step is here, as far as I know I have set up my spinner in XML, in Java I have identified that spinner, created an ArrayAdapter for said spinner, and specified some options. I'm not to sure if I have populated the spinner yet or how to manuever with the spinner object. I'd like to be able to use the spinner object to select one of three options and then keep that value inside of the textview inside of the spinner.
推荐答案
您快到了。正如你所看到的, onItemSelected
会给你一个位置
参数,你可以用它来从检索对象的适配器,如 getItemAtPosition(位置)
。
You're almost there. As you can see, the onItemSelected
will give you a position
parameter, you can use this to retrieve the object from your adapter, as in getItemAtPosition(position)
.
例如:
spinner.setOnItemSelectedListener(this);
...
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
这将会把一个信息屏幕上,与它的toString()方法打印选定的项目。
This will put a message on screen, with the selected item printed by its toString() method.
这篇关于如何使用onItemSelected在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!