問題描述
我試圖讓 onClickListener 在 Spinner 上觸發(fā),但出現(xiàn)以下錯(cuò)誤:
I'm trying to get an onClickListener to fire on a Spinner, but I get the following error:
Java.lang.RuntimeException 是不要為 AdapterView 調(diào)用 setOnClickListener.您可能需要 setOnItemClickListener"
Java.lang.RuntimeException is "Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead,"
我確定我想調(diào)用 onClickListener 而不是 onItemClickListener.我在 Stack Overflow 上發(fā)現(xiàn)了其他人提出的問題,有沒有辦法將 setOnClickListener 與 Android Spinner 一起使用?
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?
答案是:
您必須設(shè)置 Click底層視圖上的監(jiān)聽器(通常是一個(gè)帶有 id 的 TextView:android.R.id.text1) 的微調(diào)器.到這樣做:
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:
在構(gòu)造函數(shù)(帶屬性)創(chuàng)建通過提供布局的微調(diào)器android.R.layout.simple_spinner_item做一個(gè) findViewById(android.R.id.text1)獲取 TextView 現(xiàn)在設(shè)置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
我已經(jīng)嘗試了那里提到的答案,但它似乎不起作用.執(zhí)行 findViewById() 后,我得到一個(gè)指向 TextView 的空指針.
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"/>
我做錯(cuò)了什么?
我是 Stack Overflow 的新手,我沒有找到任何方法可以向其他線程發(fā)布附加問題(或評(píng)論,因?yàn)槲倚枰苌俚拇?所以我開始了一個(gè)新問題.
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.
根據(jù)建議,我嘗試了這個(gè):
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
}
});
}
}
但是 LogCat 沒有顯示出有希望的結(jié)果.
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 級(jí)別 7 和 8 上對(duì)此進(jìn)行了測試,結(jié)果相同.
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);
}
}
讓我確切地知道您需要微調(diào)器的行為方式,我們可以制定出更好的解決方案.
Let me know exactly how you need the spinner to behave, and we can work out a better solution.
這篇關(guān)于在 Android 中設(shè)置微調(diào)器 onClickListener()的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!