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

所属分类: 软件编程 / Android 阅读数: 16
收藏 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实现悬浮窗体效果,显示悬浮窗口,窗口可以拖动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Andriod studio 打包aar 的方法

这篇文章主要介绍了Andriod studio 打包aar的方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

这篇文章主要介绍了Android加载loading对话框的功能及实例代码,不退出沉浸式效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中LayoutInflater.inflater()的正确打开方式

这篇文章主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Delphi在Android下使用Java库的方法

这篇文章主要介绍了Delphi在Android下使用Java库的方法,本文以Android的USB串口通讯库为例,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多