繁体   English   中英

动态TextView onClickListener Android

[英]Dynamic TextView onClickListener Android

我将onClickListener添加到带有CompoundDrawable的动态TextView ,它似乎不起作用。

for (Product p: mProductList) {     
    ...     

    TextView textView = new TextView(this);
    if (bmp != null) {
        Drawable d = new BitmapDrawable(getResources(), bmp);
        textView.setText(name);
        textView.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);
        textView.setPadding(5, 0, 5, 0);
        textView.setTag(unique);
        textView.setClickable(true);
        textView.setOnClickListener(mProductListener);
        mProductCarousel.addView(textView, sortOrder);
    }
}

mProductListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.v("TESTING", "CLICKED");
        String productId = String.valueOf(v.getTag());
        Log.v("PRODUCT ID", productId);
    }
};

带有DrawableTextView Drawable地添加到布局中,但是click事件没有触发。 我从未收到任何日志消息。 任何帮助表示赞赏。

我不确定。 这只是预感,让我知道是否值得:

if (bmp != null) {
    Drawable d = new BitmapDrawable(getResources(), bmp);
    textView.setText(name);
    textView.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);
    textView.setPadding(5, 0, 5, 0);
    textView.setTag(unique);
    textView.setClickable(true);
    textView.setFocusableInTouchMode(true);
    textView.setOnClickListener(mProductListener);
    mProductCarousel.addView(textView, sortOrder);
}

如果没有尝试替代方法:

textView.setFocusable(true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM