博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android捕获ListView中每个item点击事件
阅读量:5902 次
发布时间:2019-06-19

本文共 5022 字,大约阅读时间需要 16 分钟。

package com.wps.android; import java.util.ArrayList;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView; public class Layouts extends Activity {    /** Called when the activity is first created. */    private ListView mylistview;    private ArrayList
list = new ArrayList
(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mylistview = (ListView)findViewById(R.id.listview); list.add("LinearLayout"); list.add("AbsoluteLayout"); list.add("TableLayout"); list.add("RelativeLayout"); list.add("FrameLayout"); ArrayAdapter
myArrayAdapter = new ArrayAdapter
(this,android.R.layout.simple_list_item_1,list); mylistview.setAdapter(myArrayAdapter); /*mylistview.setOnTouchListener(new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if(event.getAction() == MotionEvent.ACTION_DOWN) { mylistview.setBackgroundColor(Color.BLUE); } return false; } });*/ mylistview.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView
arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub if(list.get(arg2).equals("LinearLayout")) { Intent intent = new Intent("com.wps.android.LINEARLAYOUT"); startActivity(intent); } if(list.get(arg2).equals("AbsoluteLayout")) { Intent intent = new Intent("com.wps.android.ABSOLUTELAYOUT"); startActivity(intent); } if(list.get(arg2).equals("TableLayout")) { Intent intent = new Intent("com.wps.android.TABLELAYOUT"); startActivity(intent); } if(list.get(arg2).equals("RelativeLayout")) { Intent intent = new Intent("com.wps.android.RELATIVELAYOUT"); startActivity(intent); } if(list.get(arg2).equals("FrameLayout")) { Intent intent = new Intent("com.wps.android.FRAMELAYOUT"); startActivity(intent); } } }); }}

 

package
com.wps.android;
 
import
java.util.ArrayList;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.AdapterView;
import
android.widget.AdapterView.OnItemClickListener;
import
android.widget.ArrayAdapter;
import
android.widget.ListView;
 
public
class
Layouts
extends
Activity {
    
/** Called when the activity is first created. */
    
private
ListView mylistview;
    
private
ArrayList<String> list =
new
ArrayList<String>();
    
@Override
    
public
void
onCreate(Bundle savedInstanceState) {
        
super
.onCreate(savedInstanceState);
        
setContentView(R.layout.main);
        
mylistview = (ListView)findViewById(R.id.listview);
        
list.add(
"LinearLayout"
);
        
list.add(
"AbsoluteLayout"
);
        
list.add(
"TableLayout"
);
        
list.add(
"RelativeLayout"
);
        
list.add(
"FrameLayout"
);
        
ArrayAdapter<String> myArrayAdapter =
new
ArrayAdapter<String>
                            
(
this
,android.R.layout.simple_list_item_1,list);
        
mylistview.setAdapter(myArrayAdapter);
        
/*mylistview.setOnTouchListener(new OnTouchListener(){
 
            
@Override
            
public boolean onTouch(View v, MotionEvent event) {
                
// TODO Auto-generated method stub
                
if(event.getAction() == MotionEvent.ACTION_DOWN)
                
{
                    
mylistview.setBackgroundColor(Color.BLUE);
                
}
                
return false;
            
}
             
        
});*/
        
mylistview.setOnItemClickListener(
new
OnItemClickListener(){
 
            
@Override
            
public
void
onItemClick(AdapterView<?> arg0, View arg1,
int
arg2,
                    
long
arg3) {
                
// TODO Auto-generated method stub
                
if
(list.get(arg2).equals(
"LinearLayout"
))
                
{
                    
Intent intent =
new
Intent(
"com.wps.android.LINEARLAYOUT"
);
                    
startActivity(intent);
                
}
                
if
(list.get(arg2).equals(
"AbsoluteLayout"
))
                
{
                    
Intent intent =
new
Intent(
"com.wps.android.ABSOLUTELAYOUT"
);
                    
startActivity(intent);
                
}
                
if
(list.get(arg2).equals(
"TableLayout"
))
                
{
                    
Intent intent =
new
Intent(
"com.wps.android.TABLELAYOUT"
);
                    
startActivity(intent);
                
}
                
if
(list.get(arg2).equals(
"RelativeLayout"
))
                
{
                    
Intent intent =
new
Intent(
"com.wps.android.RELATIVELAYOUT"
);
                    
startActivity(intent);
                
}
                
if
(list.get(arg2).equals(
"FrameLayout"
))
                
{
                    
Intent intent =
new
Intent(
"com.wps.android.FRAMELAYOUT"
);
                    
startActivity(intent);
                
}
            
}
             
        
});
    
}
}

转载地址:http://pbkpx.baihongyu.com/

你可能感兴趣的文章
三种判断端口存活的方法和链接200的判断方法
查看>>
我的友情链接
查看>>
ftp协议基础
查看>>
顺时针打印矩阵
查看>>
JAXB
查看>>
端口聚合配置
查看>>
访问共享经常中断
查看>>
当你有一个锤子,你看什么都像钉子
查看>>
一个很实用的samba案例
查看>>
100个MySQL的调节和优化的提示
查看>>
HostMonster主机修改文件权限的方法
查看>>
人生的交易
查看>>
TP5中关联模型的使用详解
查看>>
springMVC注解之入门
查看>>
不用花钱!Android模拟器让你在电脑上免费体验谷歌手机
查看>>
MySql
查看>>
设置EditText光标位置
查看>>
OrderBy排序和IComparer的使用
查看>>
[转载]项目风险管理七种武器-拳头
查看>>
[摘录]第三章 打破部门门墙,优化团队绩效
查看>>