将Click事件添加到int数组中

时间:2014-03-04 23:03:37

标签: android

有三个图标。我把图标放在整数数组上。我怎样才能给他们点击监听器事件。 icon0,icon1,icon2事件不同。我想给他们点击事件。但我不能这样做。如何单独将点击事件分配给图标

我使用wheel.gama jar,这个图标不在xml中。它们位于可绘制文件夹

package com.myproject.gama;

import java.util.Arrays;

import com.digitalaria.gama.wheel.Wheel;
import com.digitalaria.gama.wheel.WheelAdapter;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.view.View.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;

import android.util.Log;

public class SampleWheelActivity extends Activity {

    private static final String TAG = SampleWheelActivity.class.getSimpleName();

    private Wheel wheel;
    public WheelAdapter<Adapter> adapter;
    private Resources res; 
    public int[] icons = { 
         R.drawable.icon1, R.drawable.icon0 , R.drawable.icon2};
    ImageView t;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        init();
    }

    private void init() {
        res = getApplicationContext().getResources();
        wheel = (Wheel) findViewById(R.id.wheel);       
        wheel.setItems(getDrawableFromData(icons));
        wheel.setWheelDiameter(400);
    }

    @Override
    public void onResume(){

        for (int i = 0; i < icons.length; i++) {
             t= new ImageView(SampleWheelActivity.this);
                t.setId(i);
                t.setOnClickListener((OnClickListener) this);
                    super.onResume();
    }



}

1 个答案:

答案 0 :(得分:1)

drawables没有onClick-Events。您需要将事件监听器设置为Wheel(例如OnWheelChangedListener),然后处理该事件。在那里,您可以打开选定的车轮ID。

编辑:

代码:

wheel.addChangingListener(new OnWheelChangedListener() {
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        switch(newValue) {
            case 0:
                // icon1 is selected as it has index 0
                // do something
                break;
            case 1:
                // icon0 is selected as it has index 1
                // do something else
                break;
            case 2:
                // icon2 is selected as it has index 2
                // and again something else
                break;
        }
    }
}