如何在ContextMenu中创建EditText

时间:2017-07-31 13:57:06

标签: java android listview

我有一个ListView值,我想通过按下它并打开上下文菜单来使每个值都可编辑。如何在单击ListView中的项目时显示的上下文菜单中添加EditText字段?如果您对所述问题有更好的了解,请随时提出建议。提前感谢任何回答的人!

1 个答案:

答案 0 :(得分:0)

您不必使用上下文菜单。您可以使用AlertDialog。 在这里查看这个答案 https://stackoverflow.com/a/45352961/8200290

只需使用编辑视图创建不同的布局。

这是你的解决方案。

您将需要编辑内容以使其适合您的应用程序!

MainActivity类

# DEFINITIONS

def f(x, y, z, w): return ..  . # x inner integration, ..., w outer integration
def xb(y,z,w): return (...,...) # or simply xb=(...,...) if it's a constant
def yb(z,w): return (...,...)   # or yb=(...,...)
def zb(w): return (...,...)     # or zb=(...,...)
wb = (...,...)

# INTEGRATION

result, _ = nquad(f, [xb, yb, zb, wb])

activity_main.xml中

public class MainActivity extends AppCompatActivity {

    List<String> list = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list.add("Android");
        list.add("iPhone");
        list.add("Windows");
        list.add("Blackberry");
        list.add("Mac");
        list.add("Laptop");
        list.add("LCD");
        list.add("Dell");

        ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_view_item, list);
        ListView listView = (ListView) findViewById(R.id.mobile_list);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "Clicked: " + list.get(position), Toast.LENGTH_SHORT).show();




                AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
                View alertView = getLayoutInflater().inflate(R.layout.custom_alert, null);


                //Set the view
                alert.setView(alertView);
                //Show alert
                final AlertDialog alertDialog = alert.show();
                //Can not close the alert by touching outside.
                alertDialog.setCancelable(false);
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

                //Set the edit text for the item clicked
                EditText editText = (EditText) alertView.findViewById(R.id.editText);
                editText.setText(list.get(position));

                ImageView closeButton = (ImageView) alertView.findViewById(R.id.closeButton);

                closeButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();
                    }
                });
            }
        });

        listView.setAdapter(adapter);
    }
}

list_view_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.vzw.www.listviewalert.MainActivity">

    <ListView
        android:id="@+id/mobile_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

custom_alert.xml

- &GT;这将显示您的编辑文本。底部的框关闭警报

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold" >
</TextView>

@绘制/ custom_alert_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/alertContainer"
        android:background="@drawable/custom_alert_bg">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/rowOne">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/editText"/>

        </LinearLayout>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </RelativeLayout>

    <ImageView
        android:id="@+id/closeButton"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_weight="0.33"
        android:background="#cdcdcd" />

</RelativeLayout>

您将获得以下内容:

enter image description here

enter image description here

enter image description here