Android中實(shí)現(xiàn)長(zhǎng)按修改ListView對(duì)象的內(nèi)容
實(shí)現(xiàn)的效果如下:
我在ListView的Item長(zhǎng)按事件內(nèi)打開(kā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開(kāi)發(fā)者們能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
- Android List(集合)中的對(duì)象以某一個(gè)字段排序案例
- Android使用FontMetrics對(duì)象計(jì)算位置坐標(biāo)
- Android使用Canvas對(duì)象實(shí)現(xiàn)刮刮樂(lè)效果
- 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ì)象的四種引用類(lèi)型
相關(guān)文章
android中AutoCompleteTextView的簡(jiǎn)單用法(實(shí)現(xiàn)搜索歷史)
本篇文章主要介紹了android中AutoCompleteTextView的簡(jiǎn)單用法(自動(dòng)提示),有需要的可以了解一下。2016-11-11
支持多項(xiàng)選擇的ExpandableListView
這篇文章主要為大家詳細(xì)介紹了支持多項(xiàng)選擇的ExpandableListView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android拍照保存在系統(tǒng)相冊(cè)不顯示的問(wèn)題解決方法
我們保存相冊(cè)到Android手機(jī)的時(shí)候,然后去打開(kāi)系統(tǒng)圖庫(kù)找不到我們想要的那張圖片,那是因?yàn)槲覀儾迦氲膱D片還沒(méi)有更新的緣故,下面與大家分享下此問(wèn)題的解決方法2013-06-06
flutter實(shí)現(xiàn)掃碼槍獲取數(shù)據(jù)源禁止系統(tǒng)鍵盤(pán)彈窗示例詳解
這篇文章主要為大家介紹了flutter實(shí)現(xiàn)掃碼槍獲取數(shù)據(jù)源禁止系統(tǒng)鍵盤(pán)彈窗示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android獲取驗(yàn)證碼倒計(jì)時(shí)實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android獲取驗(yàn)證碼倒計(jì)時(shí)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android開(kāi)發(fā)手冊(cè)shape屬性和子屬性使用說(shuō)明
這篇文章主要為大家介紹了Android開(kāi)發(fā)手冊(cè)shape屬性和子屬性使用說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android開(kāi)發(fā)實(shí)現(xiàn)查詢(xún)遠(yuǎn)程服務(wù)器的工具類(lèi)QueryUtils完整實(shí)例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)查詢(xún)遠(yuǎn)程服務(wù)器的工具類(lèi)QueryUtils,涉及Android服務(wù)器請(qǐng)求發(fā)送、接收、數(shù)據(jù)交互等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11

