問題描述
我試圖讓 onClickListener 在 Spinner 上觸發(fā),但出現(xiàn)以下錯誤:
I'm trying to get an onClickListener to fire on a Spinner, but I get the following error:
Java.lang.RuntimeException 是不要為 AdapterView 調用 setOnClickListener.您可能需要 setOnItemClickListener"
Java.lang.RuntimeException is "Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead,"
我確定我想調用 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?
答案是:
您必須設置 Click底層視圖上的監(jiān)聽器(通常是一個帶有 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:
在構造函數(shù)(帶屬性)創(chuàng)建通過提供布局的微調器android.R.layout.simple_spinner_item做一個 findViewById(android.R.id.text1)獲取 TextView 現(xiàn)在設置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() 后,我得到一個指向 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().
這就是我正在做的:
文件 layoutspinner.xml
我做錯了什么?
我是 Stack Overflow 的新手,我沒有找到任何方法可以向其他線程發(fā)布附加問題(或評論,因為我需要很少的代表)所以我開始了一個新問題.
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ù)建議,我嘗試了這個:
Per recomendation I tried this:
但是 LogCat 沒有顯示出有希望的結果.
But LogCat isn't showing promising results.
我在 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.
讓我確切地知道您需要微調器的行為方式,我們可以制定出更好的解決方案.
Let me know exactly how you need the spinner to behave, and we can work out a better solution.
這篇關于在 Android 中設置微調器 onClickListener()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!