Android 针对ListActivity中ListView 点击事件和长按事件

最近在学习android,对Android有些关键的知识点进行记录,以便后面的学习。

在创建的Activity如果继承自ListActivity类,则其默认拥有一个Listview控件提供使用, 下面主要就Listview中,item点击事件和长按事件的进行说明。

1、首先获得listview实例;

[java]  view plain copy print ?
  1. ListView lv = getListView();  

2、添加点击事件

  对lv添加点击监听器。

[java]  view plain copy print ?
  1. lv.setOnItemClickListener(new OnItemClickListener() {  
  2.             public void onItemClick(AdapterView<?> parent, View view,  
  3.                     int position, long id) {  
  4.                 // When clicked, show a toast with the TextView text  
  5.                 TextView tv = (TextView)view.findViewById(R.id.textViewVideoTitle);  
  6.                 Toast.makeText(getApplicationContext(),  
  7.                         tv.getText(), Toast.LENGTH_SHORT).show();  
  8.             }  
  9.         });  
其中 R.id.textViewVideoTitle 是Listview,item中的一个TextView控件。
3、添加长按事件

[java]  view plain copy print ?
  1. lv.setOnItemLongClickListener(new OnItemLongClickListener(){  
  2.             @Override  
  3.             public boolean onItemLongClick(AdapterView<?> arg0, View arg1,  
  4.                     int arg2, long arg3) {  
  5.                 // TODO Auto-generated method stub  
  6.                 // When clicked, show a toast with the TextView text  
  7.                 TextView tv = (TextView)arg1.findViewById(R.id.textViewVideoTitle);  
  8.                 Toast.makeText(getApplicationContext(),  
  9.                         tv.getText(), Toast.LENGTH_SHORT).show();  
  10.                 return false;  
  11.             }  
  12.         });  

备注:

对于缺少的包,在eclipse中按Ctrl+shift+O就可以自动引入。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值