gpt4 book ai didi

android - 如何在 Android 中使用 onItemSelected?

转载 作者:IT王子 更新时间:2023-10-29 00:10:50 24 4
gpt4 key购买 nike

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 程序员,在使用 Spinner 时遇到了麻烦,我尝试了多个教程,但仍然感到困惑。我想知道我的下一步是什么,据我所知,我已经在 XML 中设置了我的微调器,在 Java 中我已经确定了该微调器,为所述微调器创建了一个 ArrayAdapter,并指定了一些选项。我不确定我是否已经填充了微调器或如何使用微调器对象进行操作。我希望能够使用微调器对象来选择三个选项之一,然后将该值保留在微调器内的 textview 内。

最佳答案

你快到了。如您所见,onItemSelected 将为您提供一个 position 参数,您可以使用它从适配器中检索对象,如 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() 方法打印所选项目。

关于android - 如何在 Android 中使用 onItemSelected?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20151414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com