实现的效果如下:
我在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; @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>
总结
以上就是这篇文章的全部内容了,希望本文的内容对各位Android开发者们能带来一定的帮助,如果有疑问大家可以留言交流。
新闻热点
疑难解答