本文共 1960 字,大约阅读时间需要 6 分钟。
我在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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);lvShow = (ListView) findViewById(R.id.lvShow);String[] arr = { "李四", "小猪", "店小二" };ArrayAdapterAdap1 = new ArrayAdapter (this,R.layout.test_list, arr);lvShow.setAdapter(Adap1);// 设置ListView的显示lvShow.setOnItemLongClickListener(new OnItemLongClickListener() {@Overridepublic 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() {// 设置按钮的点击事件@Overridepublic void onClick(View v) {((TextView) view).setText(edit.getText().toString());dialog.dismiss();}});dialog = new AlertDialog.Builder(MainActivity.this).setView(contview).create();}}