如何在动态生成的文本视图中添加onclicklistener [英] How to add onclicklistener to dynamically generated text views

查看:75
本文介绍了如何在动态生成的文本视图中添加onclicklistener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用程序中动态创建了Textviews.我想对此文本视图赋予单击时"事件.单击文本视图时,我需要获取文本视图的ID.

I have dynamically created Textviews in my application. I want to give On click event to this textviews.. When I click on the textview, I need to get the id of the textview..

推荐答案

您尝试使用以下代码.

TextView text = new TextView(this);

    text.setText("text here");
    ll.addView(text);
    text.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "ID : "+arg0.getId(), Toast.LENGTH_SHORT).show();
        }
    });

其中"ll"是布局,它添加textview并为click事件添加put clickListener().

in that "ll" is Layout which add textview and after adding put clickListener() for click event.

希望这对您有用.

这篇关于如何在动态生成的文本视图中添加onclicklistener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆