久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Android 中設(shè)置微調(diào)器 onClickListener()

Setting a spinner onClickListener() in Android(在 Android 中設(shè)置微調(diào)器 onClickListener())
本文介紹了在 Android 中設(shè)置微調(diào)器 onClickListener()的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我試圖讓 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 調(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)聽器(通常是一個帶有 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做一個 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() 后,我得到一個指向 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"/>

我做錯了什么?

我是 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:

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 級別 7 和 8 上對此進行了測試,結(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()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 亚洲视频一区在线观看 | 日日骚av | 久久精品伊人 | 97精品视频在线观看 | 亚洲视频一区二区三区四区 | 久久在线视频 | 久久99久久久 | 欧美精品网站 | 亚洲经典一区二区三区 | 亚洲一级黄色片 | 国产精品国产成人国产三级 | 欧美黄色一区二区 | 成人一区二区三区四区 | 日韩视频在线观看免费 | av手机在线免费观看 | 夜夜欢视频 | 午夜精品免费 | 国产福利视频在线观看 | 91成人在线观看喷潮蘑菇 | 日本久久久久久 | 欧美精品系列 | 国产偷自拍 | 国产成人91 | 中文字幕一二区 | 激情综合五月天 | 午夜免费av| 欧美少妇视频 | 国产wwwwww| 亚洲欧美日韩一区 | 国产二区视频在线观看 | 日本黄色免费网站 | 成人毛片在线观看 | 日韩免费看片 | 97超碰资源总站 | 欧美一级做性受免费大片免费 | 日韩免费精品视频 | 国产日批视频 | 亚欧洲精品在线视频免费观看 | 久久r| 国产美女自拍 | 欧美丰满少妇 |