设置一个微调onClickListener()的安卓 [英] Setting a spinner onClickListener() in Android
问题描述
我试图让一个onClickListener火在微调,但我得到了以下错误:
I'm trying to get an onClickListener to fire on a Spinner, but I get the following error:
java.lang.RuntimeException的是不要叫setOnClickListener了一个AdapterView。你可能想setOnItemClickListener相反,
Java.lang.RuntimeException is "Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead,"
我敢肯定,我想打电话给onClickListener和NOT onItemClickListener。我发现问别人对堆栈溢出的问题,的 有没有办法使用setOnClickListener与Android的微调?
I'm sure I want to call onClickListener and NOT onItemClickListener. I found a question asked by someone else on Stack Overflow, Is there a way to use setOnClickListener with an Android Spinner?
答案提到有:
您必须设置点击 听众底层视图 (通常是一个id为TextView的: android.R.id.text1)微调的。至 这样做的:
You will have to set the Click listener on the underlying view (normally a TextView with id: android.R.id.text1) of the spinner. To do so:
创建自定义微调在 构造器(带属性)创建 微调通过提供布局 android.R.layout.simple_spinner_item 做一个findViewById(android.R.id.text1) 得到的TextView现在设置 onClickListener到的TextView
Create a custom Spinner In the constructor (with attributes) create the spinner by supplying the layout android.R.layout.simple_spinner_item Do a findViewById(android.R.id.text1) to get the TextView Now set the onClickListener to the TextView
我试图回答说有,但似乎并没有工作。我得到一个空指针的TextView后,我做的findViewById()。
I have tried the answer noted there, but it doesn't seem to work. I get a null pointer to the TextView after I do the findViewById().
这是我在做什么:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.layoutspinner,dataArray);
spinner.setAdapter(adapter);
TextView SpinnerText = (TextView)findViewById(R.id.spinnerText);
if (SpinnerText == null) {
System.out.println("Not found");
}
else {
SpinnerText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Do something
}
});
}
文件layoutspinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerText"
android:singleLine ="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="6pt"
android:gravity="right"/>
我是什么做错了吗?
What am I doing wrong?
我是新来的堆叠式和NBSP;溢出,我没有发现任何方式发布一个aditional的问题,给其他线程(或评论,因为我有一点代表),所以我开始了一个新的问题
I'm new to Stack Overflow, I didn't find any way to post an aditional question to the other thread (or comment since I have to little rep) so I started a new question.
每recomendation我尝试这样做:
Per recomendation I tried this:
int a = spinnerMes.getCount();
int b = spinnerMes.getChildCount();
System.out.println("Count = " + a);
System.out.println("ChildCount = " + b);
for (int i = 0; i < b; i++) {
View v = spinnerMes.getChildAt(i);
if (v == null) {
System.out.println("View not found");
}
else {
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Click code
}
});
}
}
But LogCat isn't showing promising results.
10-14 16:09:08.127: INFO/System.out(3116): Count = 7
10-14 16:09:08.127: INFO/System.out(3116): ChildCount = 0
我已经在API级别7和8相同的结果测试这一点。
I have tested this on API levels 7 and 8 with the same results.
推荐答案
下面的工作你想要的,但它并不理想。
The following works how you want it, but it is not ideal.
public class Tester extends Activity {
String[] vals = { "here", "are", "some", "values" };
Spinner spinner;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner = (Spinner) findViewById(R.id.spin);
ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, vals);
spinner.setAdapter(ad);
Log.i("", "" + spinner.getChildCount());
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
int a = spinner.getCount();
int b = spinner.getChildCount();
System.out.println("Count =" + a);
System.out.println("ChildCount =" + b);
for (int i = 0; i < b; i++) {
View v = spinner.getChildAt(i);
if (v == null) {
System.out.println("View not found");
} else {
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("","click");
}
});
}
}
}
}, 500);
}
}
让我知道你到底有需要微调的行为,我们可以制定出一个更好的解决方案。
Let me know exactly how you need the spinner to behave, and we can work out a better solution.
这篇关于设置一个微调onClickListener()的安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!