Android中實現(xiàn)長按修改ListView對象的內(nèi)容
更新時間:2017年02月23日 10:45:10 作者:chaoyu168
這篇文章主要給大家介紹了在Android中實現(xiàn)長按修改ListView對象內(nèi)容的相關(guān)資料,文中給出了完整的示例代碼,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
實現(xiàn)的效果如下:
我在ListView的Item長按事件內(nèi)打開一個彈出窗口,窗口內(nèi)有一個EditText對象,在這個編輯框內(nèi)輸入文本點確定后,直接修改掉ListView對象內(nèi)某個TextView對象的內(nèi)容。
示例代碼如下:
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>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對各位Android開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
Android使用Shape實現(xiàn)ProgressBar樣式實例
本篇文章主要介紹了Android使用Shape實現(xiàn)ProgressBar樣式實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04Android中RecyclerView實現(xiàn)簡單購物車功能
這篇文章主要為大家詳細介紹了Android中RecyclerView實現(xiàn)簡單購物車功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Android應用中使用DOM方式解析XML格式數(shù)據(jù)的基本方法
這篇文章主要介紹了Android應用中使用DOM方式解析XML格式數(shù)據(jù)的基本方法,值得注意的是DOM方式解析的效率并不高,在數(shù)據(jù)量大的時候并不推薦使用,需要的朋友可以參考下2016-04-04【Android 基礎】詳解Animation 動畫介紹和實現(xiàn)
這篇文章主要介紹了【Android 基礎】詳解Animation 動畫介紹和實現(xiàn) ,對于想要學習android開發(fā)的同學具有一定的參考價值,有需要的可以了解一下。2016-12-12