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

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

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

問題描述

我試圖讓 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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,如何獲取插入的自動(dòng)生成密鑰?[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 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 在线视频h| 亚洲精品一区二区二区 | 亚洲免费视频在线观看 | 91欧美激情一区二区三区成人 | 久久一 | 欧美在线一区二区三区 | 久久精品国产久精国产 | 国产精品一区二区无线 | 99亚洲精品 | 国产精品中文字幕在线观看 | 亚洲日韩中文字幕一区 | 久久久久网站 | 欧美乱淫视频 | 日韩在线不卡 | 暖暖日本在线视频 | 黄色日本片| 中文字幕在线免费观看 | 亚洲精品粉嫩美女一区 | 国产精品久久久久久久久久久久 | 欧美成年黄网站色视频 | 红色av社区| 成人在线视频免费观看 | 国产高清精品一区二区三区 | 欧美午夜精品久久久久久浪潮 | 国产精品久久欧美久久一区 | 少妇av片 | 中文字幕在线视频免费观看 | 四虎成人免费电影 | 91精品国产91久久久久久 | 欧美一级免费 | 国产一区二区三区在线 | 国产成人精品一区二区三区四区 | 色综合久久久久 | 国产探花| 欧美日韩精品国产 | www精品美女久久久tv | 国产精品久久久久久久久免费桃花 | 91国内精精品久久久久久婷婷 | 中日韩欧美一级片 | 久久新| 日本五月婷婷 |