setOnClickListener

package com.yuchongxia.internetradio.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.yuchongxia.internetradio.R;

public class MessageView extends LinearLayout {
    private OnClickListener ml;

    public MessageView(Context context) {
        super(context);
        LayoutInflater.from(getContext()).inflate(R.layout.message_view, this);
        findViewById(R.id.button_refresh).setOnClickListener(onClickListener);
    }

    public MessageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(getContext()).inflate(R.layout.message_view, this);
        findViewById(R.id.button_refresh).setOnClickListener(onClickListener);
    }

    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (ml != null) {
                ml.onClick(v);
            }
        }
    };

    @Override
    public void setOnClickListener(OnClickListener l) {
        ml = l;
        super.setOnClickListener(ml);
    }
}
package com.yuchongxia.internetradio.widget;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.yuchongxia.internetradio.R;
import com.yuchongxia.internetradio.constant.Constant;
import com.yuchongxia.internetradio.pojo.ContentCategory;
import com.yuchongxia.internetradio.util.HttpClientUtil;
import com.yuchongxia.internetradio.util.Util;

/**
 * 分类导航组件
 *
 * @author Andy
 * @date 2013-9-3
 */
public class NavigationBar extends RelativeLayout {
    private Context context;
    private RelativeLayout.LayoutParams params;
    private RelativeLayout container;
    // 焦点所在位置
    private int focusPosition = 1;
    private OnMyClickListener onMyClickListener;
    private OnDataPreparedListener onDataPreparedListener;
    private List<ContentCategory> list;

    // private ContentCategoryDao contentCategoryDao;

    public NavigationBar(Context context, AttributeSet attrs) throws IOException, JSONException {
        super(context, attrs);
        this.context = context;
        LayoutInflater.from(getContext()).inflate(R.layout.navigation_bar, this);
        container = (RelativeLayout) findViewById(R.id.layout_navigationBar);
        list = new ArrayList<ContentCategory>();
        // contentCategoryDao = new ContentCategoryDao(context);
        if (Util.isNetworkAvailable(context)) {
            getRingCategory();
        }
    }

    // public void writeDB() {
    // new Thread() {
    // @Override
    // public void run() {
    // super.run();
    // // contentCategoryDao.insert(list);
    // }
    // }.start();
    // }

    public void parseJson(final String data) {
        new Thread() {
            @Override
            public void run() {
                try {
                    JSONArray jsonArray = new JSONArray(data);
                    if (jsonArray == null || jsonArray.length() == 0) {
                        handler.sendEmptyMessage(0);
                        return;
                    }

                    Log.i(Constant.TAG, "开始解析网络获取的数据");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        ContentCategory contentCategory = new ContentCategory();
                        JSONObject object = (JSONObject) jsonArray.getJSONObject(i);

                        int categoryId = object.getInt("categoryId");
                        String categoryName = object.getString("categoryName");
                        int sort = object.getInt("order");
                        int publishTime = object.getInt("publishTime");

                        contentCategory.setCategoryId(categoryId);
                        contentCategory.setCategoryName(categoryName);
                        contentCategory.setSort(sort);
                        contentCategory.setPublishTime(publishTime);

                        list.add(contentCategory);
                    }

                    Log.i(Constant.TAG, "解析后数据  jsList size:" + list.size());

                    // 全部
                    ContentCategory temp = new ContentCategory();
                    temp.setCategoryId(-1);
                    temp.setCategoryName(context.getString(R.string.all));
                    list.add(0, temp);

                    temp = new ContentCategory();
                    temp.setCategoryId(-2);
                    temp.setCategoryName(context.getString(R.string.collect));
                    list.add(0, temp);

                    temp = new ContentCategory();
                    temp.setCategoryId(-3);
                    temp.setCategoryName(context.getString(R.string.local_radio));
                    list.add(temp);

                    temp = new ContentCategory();
                    temp.setCategoryId(-4);
                    temp.setCategoryName(context.getString(R.string.more));
                    list.add(temp);

                    handler.sendEmptyMessage(1);
                } catch (JSONException e) {
                    Log.i(Constant.TAG, "解析数据时出错");
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void getRingCategory() {
        new Thread() {
            @Override
            public void run() {
                String data = HttpClientUtil.get(Constant.CONTENT_CATEGOARY_URL);
                parseJson(data);
            }
        }.start();
    }

    public void layout() {
        container.removeAllViews();
        NavigationButton btn;

        int cnt = list.size();
        if (onDataPreparedListener != null) {
            Log.i(Constant.TAG, "prepared");
            onDataPreparedListener.onPrepared(list);
        }

        for (int i = 0; i < cnt; i++) {
            params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            btn = new NavigationButton(context);
            btn.setId(i + 1);
            btn.setText(list.get(i).getCategoryName());
            btn.setOnClickListener(clickListener);
            if (i == 0) {
                btn.setChoosed(true);
            }

            String language = Locale.getDefault().getLanguage();
            String country = Locale.getDefault().getCountry();
            // zh CN
            // zh TW
            // en **
            if (language.equals("zh") && country.equals("CN")) {
            } else if (language.equals("zh") && country.equals("TW")) {
            } else {
            }

            if (i > 0) {
                params.addRule(RelativeLayout.BELOW, i);
            }
            container.addView(btn, params);
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                Log.i(Constant.TAG, "无法获取数据");
            } else if (msg.what == 1) {
                layout();
            }
        }
    };

    /**
     * 刷新NavigationBar
     */
    public void refresh() {
        getRingCategory();
        // 恢复当前位置标示
        focusPosition = 1;
    }

    /**
     * 取 NavigationBar 第一项的分类id
     *
     * @return 第一项的分类id
     */
    // public String getFirst() {
    // if (list.size() != 0) {
    // return list.get(0).getCategoryId();
    // } else {
    // return "";
    // }
    // }

    /**
     * 焦点是否在第一项
     */
    public boolean isFocusOnFirst() {
        return focusPosition == 1 ? true : false;
    }

    public void setChoosed(int id) {
        NavigationButton btn = (NavigationButton) findViewById(focusPosition);
        btn.setChoosed(false);
        btn = (NavigationButton) findViewById(id);
        btn.setChoosed(true);
        // 标记当前选中项
        focusPosition = id;
    }

    OnClickListener clickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            // 取消选中
            NavigationButton btn = (NavigationButton) findViewById(focusPosition);
            btn.setChoosed(false);
            focusPosition = id;
            // 重新选中
            btn = (NavigationButton) findViewById(id);
            btn.setChoosed(true);

            if (onMyClickListener != null && list.size() != 0) {
                onMyClickListener.onClick(id - 1);
            }
        }
    };

    /**
     * 回调接口
     *
     */
    public interface OnDataPreparedListener {
        public void onPrepared(List<ContentCategory> list);
    }

    public interface OnMyClickListener {
        public void onClick(int buttonId);
    }

    /**
     * 设置一个监听,供外部调用的方法
     */
    public void setOnMyClickListener(OnMyClickListener listener) {
        this.onMyClickListener = listener;
    }

    /**
     * 设置一个监听,供外部调用的方法
     */
    public void setOnDataPreparedListener(OnDataPreparedListener onDataPreparedListener) {
        this.onDataPreparedListener = onDataPreparedListener;
    }
}

setOnClickListener

时间: 2024-09-01 21:04:33

setOnClickListener的相关文章

BaseAdapter的getView方法中对setOnclickListener优化

在应用开发中凡是又列表式的界面我都采取使用ListView来呈现界面,并且需要一个BaseAdapter来加载数据进去,所以我们就需要重写getView方法,那么问题来了,在每一个Item都有一个按钮的话我们需要给按钮setOnclickListener,假设我们按一下代码来写的话: ActiveClick ac = new ActiveClick(); convertView.findViewById(R.id.main).setOnClickListener(ac); 通过打印我们可以看到l

setOnClickListener报空指针异常

1.异常提示: 2.错误原因: 先看代码: public class MainActivity extends ActionBarActivity { private Button btn_test; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_test =

View的setOnClickListener的添加方法

1)第一种,也是最长见的添加方法(一下都以Button为例) 1 Button btn = (Button) findViewById(R.id.myButton);2 btn .setOnClickListener(new View.OnClickListener() {3 public void onClick(View v) {4 //do something5 }6 }); 2)第二种,下面这个方法较前一种稍微简单了一些,允许多个Buttons共享一个Listener.通过Switch控

android中setOnClickListener的那点事

最近在写代码中,发现在xml文件设置了android:clickable="false",之后这个View还是可点的. 后来发现,是代码中对View设置了监听事件(setOnClickListener),把代码屏蔽了,clickable属性生效. 后来查看了一下源码(setOnClickListener),代码如下,发现其中的if语句,如果这个View clickable不可点,该方法会把这个View设为可点,这也就是为什么我的问题会出现的原因了 public void setOnCl

力所能及之The method setOnClickListener(View.OnClickListener) in the type View is not

开始学习android了,写监听器老是报错,不知道为什么,翻了翻度娘 The method setOnClickListener(View.OnClickListener) in the type View is not 这种问题在调用文件上引入import android.view.View.OnClickListener; 解决!

android setOnClickListener 回调函数理解

1.模拟java View类: package com.test; /** * Created by dandan on 15-2-7. */ public class View { /**设置默认ID**/ public static final int NO_ID = -1; /**设置每个控件的ID用于被子类覆盖**/ public int mID = NO_ID; /**保存点击回调函数**/ public OnClicklistener mOnClicklistener; public

我的android学习脚步----------- Button 和监听器setonclicklistener

最基本的学习,设置一个按钮并监听实现实时时刻显示 首先XML布局,在layout中的  activity_main.xml中拖一个Button按钮到相应位置 然后在xml文件中做修改 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_wid

从零开始学android开发-View的setOnClickListener的添加方法

1)第一种,也是最长见的添加方法(一下都以Button为例) Button btn = (Button) findViewById(R.id.myButton); btn .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //do something } }); 2)第二种,下面这个方法较前一种稍微简单了一些,允许多个Buttons共享一个Listener.通过Switch控制对不同But

android的MainActivity中setOnClickListener(this)中的this指代

setOnClickListener的参数要求是一个实现了OnClickListener接口的对象实体,它可以是任何类的实例,只要该类实现了OnClickListener.你的问题中,它就是MainActivity这个对象自己. 1 findViewById(R.id.ButtonID).setOnClickListener(new View.OnclickListener(){ 2 public void onclick(View v){ 3 4 } 5 });