Android中实现长按修改ListView对象的内容
实现的效果如下:
我在ListView的Item长按事件内打开一个弹出窗口,窗口内有一个EditText对象,在这个编辑框内输入文本点确定后,直接修改掉ListView对象内某个TextView对象的内容。
示例代码如下:
importandroid.os.Bundle; importandroid.app.Activity; importandroid.app.AlertDialog; importandroid.graphics.Color; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.AdapterView; importandroid.widget.AdapterView.OnItemLongClickListener; importandroid.widget.ArrayAdapter; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.ListView; importandroid.widget.TextView; publicclassMainActivityextendsActivity{ privateListViewlvShow; privateAlertDialogdialog; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lvShow=(ListView)findViewById(R.id.lvShow); String[]arr={"李四","小猪","店小二"}; ArrayAdapter<String>Adap1=newArrayAdapter<String>(this, R.layout.test_list,arr); lvShow.setAdapter(Adap1);//设置ListView的显示 lvShow.setOnItemLongClickListener(newOnItemLongClickListener(){ @Override publicbooleanonItemLongClick(AdapterView<?>parent,Viewview, intposition,longid){ setAlertDialog(view); dialog.show(); returnfalse; } }); } privatevoidsetAlertDialog(finalViewview){ LayoutInflaterfactory=LayoutInflater.from(getApplicationContext()); //引入一个外部布局 Viewcontview=factory.inflate(R.layout.test_dialog,null); contview.setBackgroundColor(Color.BLACK);//设置该外部布局的背景 finalEditTextedit=(EditText)contview .findViewById(R.id.edit_dialog);//找到该外部布局对应的EditText控件 ButtonbtOK=(Button)contview.findViewById(R.id.btOK_dialog); btOK.setOnClickListener(newOnClickListener(){//设置按钮的点击事件 @Override publicvoidonClick(Viewv){ ((TextView)view).setText(edit.getText().toString()); dialog.dismiss(); } }); dialog=newAlertDialog.Builder(MainActivity.this).setView(contview) .create(); } }
<RelativeLayoutxmlns: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"/>
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns: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开发者们能带来一定的帮助,如果有疑问大家可以留言交流。