Android中實(shí)現(xiàn)長(zhǎng)按修改ListView對(duì)象的內(nèi)容
實(shí)現(xiàn)的效果如下:
我在ListView的Item長(zhǎng)按事件內(nèi)打開一個(gè)彈出窗口,窗口內(nèi)有一個(gè)EditText對(duì)象,在這個(gè)編輯框內(nèi)輸入文本點(diǎn)確定后,直接修改掉ListView對(duì)象內(nèi)某個(gè)TextView對(duì)象的內(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);// 設(shè)置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()); // 引入一個(gè)外部布局 View contview = factory.inflate(R.layout.test_dialog, null); contview.setBackgroundColor(Color.BLACK);// 設(shè)置該外部布局的背景 final EditText edit = (EditText) contview .findViewById(R.id.edit_dialog);// 找到該外部布局對(duì)應(yīng)的EditText控件 Button btOK = (Button) contview.findViewById(R.id.btOK_dialog); btOK.setOnClickListener(new OnClickListener() {// 設(shè)置按鈕的點(diǎn)擊事件 @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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位Android開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流。
- Android List(集合)中的對(duì)象以某一個(gè)字段排序案例
- Android使用FontMetrics對(duì)象計(jì)算位置坐標(biāo)
- Android使用Canvas對(duì)象實(shí)現(xiàn)刮刮樂效果
- Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對(duì)象的方法詳解
- Android中將Bitmap對(duì)象以PNG格式保存在內(nèi)部存儲(chǔ)中的方法
- Android中利用C++處理Bitmap對(duì)象的實(shí)現(xiàn)方法
- Android中傳遞對(duì)象的三種方法的實(shí)現(xiàn)
- Android中深入學(xué)習(xí)對(duì)象的四種引用類型
相關(guān)文章
Android使用Shape實(shí)現(xiàn)ProgressBar樣式實(shí)例
本篇文章主要介紹了Android使用Shape實(shí)現(xiàn)ProgressBar樣式實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04Android中RecyclerView實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車功能
這篇文章主要為大家詳細(xì)介紹了Android中RecyclerView實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Android Button按鈕點(diǎn)擊背景和文字變化操作
這篇文章主要介紹了Android Button按鈕點(diǎn)擊背景和文字變化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08Android應(yīng)用中使用DOM方式解析XML格式數(shù)據(jù)的基本方法
這篇文章主要介紹了Android應(yīng)用中使用DOM方式解析XML格式數(shù)據(jù)的基本方法,值得注意的是DOM方式解析的效率并不高,在數(shù)據(jù)量大的時(shí)候并不推薦使用,需要的朋友可以參考下2016-04-04android實(shí)現(xiàn)一個(gè)圖片驗(yàn)證碼倒計(jì)時(shí)功能
本文通過實(shí)例代碼給大家介紹了android實(shí)現(xiàn)一個(gè)圖片驗(yàn)證碼倒計(jì)時(shí)功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-11-11android實(shí)現(xiàn)QQ微信側(cè)滑刪除效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)QQ微信側(cè)滑刪除效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12【Android 基礎(chǔ)】詳解Animation 動(dòng)畫介紹和實(shí)現(xiàn)
這篇文章主要介紹了【Android 基礎(chǔ)】詳解Animation 動(dòng)畫介紹和實(shí)現(xiàn) ,對(duì)于想要學(xué)習(xí)android開發(fā)的同學(xué)具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12