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

 更新时间:2017年02月23日 10:45:10   作者:chaoyu168  
这篇文章主要给大家介绍了在Android中实现长按修改ListView对象内容的相关资料,文中给出了完整的示例代码,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。

实现的效果如下:

我在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 SDK Manager更新、下载速度慢问题解决办法

    Android SDK Manager更新、下载速度慢问题解决办法

    这篇文章主要介绍了Android SDK Manager更新、下载速度慢问题解决办法的相关资料,需要的朋友可以参考下
    2017-05-05
  • Android Button的基本用法详解及简单实例

    Android Button的基本用法详解及简单实例

    这篇文章主要介绍了Android Button的基本用法详解及简单实例的相关资料,需要的朋友可以参考下
    2017-02-02
  • Android UI 中的 ListView列表控件的示例

    Android UI 中的 ListView列表控件的示例

    本篇文章主要介绍了Android UI 中的 ListView列表控件的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • Android利用RecyclerView实现全选、置顶和拖拽功能示例

    Android利用RecyclerView实现全选、置顶和拖拽功能示例

    列表控件可以说是我们绝大部分App中都会使用的,为了提升交互乐趣,我们经常需要在列表中加入置顶、拖拽等操作,下面这篇文章主要介绍了Android利用RecyclerView如何实现全选、置顶和拖拽功能的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-04-04
  • Android画板开发之基本画笔功能

    Android画板开发之基本画笔功能

    这篇文章主要为大家详细介绍了Android画板开发之基本画笔功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • 使用Chrome浏览器调试Android App详解

    使用Chrome浏览器调试Android App详解

    这篇文章主要介绍了使用Chrome浏览器调试Android App详解,本网讲解了使用Facebook开源Stetho实现在Chrome中调试Android App中,需要的朋友可以参考下
    2015-05-05
  • Android实现加载状态视图切换效果

    Android实现加载状态视图切换效果

    这篇文章主要为大家详细介绍了Android实现加载状态视图切换效果的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Android神兵利器之Image Asset Studio的实现

    Android神兵利器之Image Asset Studio的实现

    这篇文章主要介绍了Android神兵利器之Image Asset Studio的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • Listview中Button抢占焦点的解决方法

    Listview中Button抢占焦点的解决方法

    在程序开发中经常见到listview button抢占焦点的问题,怎么回事什么原因呢?下面小编给大家带来了Listview中Button抢占焦点的解决方法,感兴趣的朋友一起看下吧
    2016-08-08
  • 用Kotlin实现Android点击事件的方法

    用Kotlin实现Android点击事件的方法

    本篇文章主要介绍了用Kotlin实现Android点击事件的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06

最新评论