Android中实现长按修改ListView对象的内容

所属分类: 软件编程 / Android 阅读数: 50
收藏 0 赞 0 分享

实现的效果如下:

我在ListView的Item长按事件内打开一个弹出窗口,窗口内有一个EditText对象,在这个编辑框内输入文本点确定后,直接修改掉ListView对象内某个TextView对象的内容。

示例代码如下:

import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.graphics.Color; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemLongClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 
 
public class MainActivity extends Activity { 
private ListView lvShow; 
private AlertDialog dialog; 
 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
lvShow = (ListView) findViewById(R.id.lvShow); 
String[] arr = { "李四", "小猪", "店小二" }; 
ArrayAdapter<String> Adap1 = new ArrayAdapter<String>(this, 
R.layout.test_list, arr); 
lvShow.setAdapter(Adap1);// 设置ListView的显示 
lvShow.setOnItemLongClickListener(new OnItemLongClickListener() { 
 
@Override 
public boolean onItemLongClick(AdapterView<?> parent, View view, 
int position, long id) { 
setAlertDialog(view); 
dialog.show(); 
return false; 
} 
}); 
} 
 
private void setAlertDialog(final View view) { 
LayoutInflater factory = LayoutInflater.from(getApplicationContext()); 
// 引入一个外部布局 
View contview = factory.inflate(R.layout.test_dialog, null); 
contview.setBackgroundColor(Color.BLACK);// 设置该外部布局的背景 
final EditText edit = (EditText) contview 
.findViewById(R.id.edit_dialog);// 找到该外部布局对应的EditText控件 
Button btOK = (Button) contview.findViewById(R.id.btOK_dialog); 
btOK.setOnClickListener(new OnClickListener() {// 设置按钮的点击事件 
 
@Override 
public void onClick(View v) { 
((TextView) view).setText(edit.getText().toString()); 
dialog.dismiss(); 
} 
}); 
dialog = new AlertDialog.Builder(MainActivity.this).setView(contview) 
.create(); 
} 
} 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:paddingBottom="@dimen/activity_vertical_margin" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin" 
 android:paddingTop="@dimen/activity_vertical_margin" 
 tools:context=".MainActivity" > 
 
 <ListView 
  android:id="@+id/lvShow" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" /> 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:gravity="center_horizontal" 
 android:orientation="vertical" > 
 
 <EditText 
  android:id="@+id/edit_dialog" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:textSize="28sp" /> 
 
 <Button 
  android:id="@+id/btOK_dialog" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="确定" /> 
 
</LinearLayout> 

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位Android开发者们能带来一定的帮助,如果有疑问大家可以留言交流。

更多精彩内容其他人还在看

Android 实现圆角图片的简单实例

这篇文章主要介绍了Android 实现圆角图片的简单实例的相关资料,Android 圆角图片的实现形式,包括用第三方、也有系统,需要的朋友可以参考下
收藏 0 赞 0 分享

Android实现底部半透明弹出框PopUpWindow效果

这篇文章主要为大家详细介绍了Android实现底部半透明弹出框PopUpWindow效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android二级缓存加载图片实现照片墙功能

这篇文章主要为大家详细介绍了Android二级缓存加载图片实现照片墙功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿Iphone屏幕底部弹出半透明PopupWindow效果

这篇文章主要为大家详细介绍了Android仿Iphone屏幕底部弹出半透明PopupWindow效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android编程实现的微信支付功能详解【附Demo源码下载】

这篇文章主要介绍了Android编程实现的微信支付功能,结合实例形式详细分析了Android微信支付功能的实现步骤与具体操作技巧,并附带了Demo源码供读者下载参考,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之APP安装后在桌面上不显示应用图标的解决方法

这篇文章主要介绍了Android开发之APP安装后在桌面上不显示应用图标的解决方法,涉及Android activity相关属性设置技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android RecyclerView实现数据列表展示效果

这篇文章主要为大家详细介绍了Android RecyclerView实现数据列表展示效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android Uri和文件路径互相转换的实例代码

在项目中需要用到将Uri转换为绝对路径,下面小编把Android Uri和文件路径互相转换的实例代码分享到脚本之家平台,需要的的朋友参考下吧
收藏 0 赞 0 分享

Android直播app送礼物连击动画效果(实例代码)

最近在做公司的直播项目,需要实现一个观看端连击送礼物的控件,下面给大家分享实例代码,需要的的朋友参考下吧
收藏 0 赞 0 分享

Android文件选择器ExFilePicker的使用方法

这篇文章主要为大家详细介绍了Android文件选择器ExFilePicker的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多