简体   繁体   English

如何在ContextMenu中制作EditText

[英]How can I make an EditText in a ContextMenu

I have a ListView of values and I want to make each value editable by pressing on it and opening a context menu. 我有一个值的ListView,我想通过按每个值并打开上下文菜单来使其可编辑。 How can I add an EditText field to the context menu that appears when clicking on an item in a ListView? 如何在单击ListView中的项目时将EditText字段添加到出现的上下文菜单中? If you have a better idea for said issue, feel free to suggest it. 如果您对上述问题有更好的主意,请随时提出建议。 Thanks in advance to anyone who answers! 预先感谢任何回答!

You don't have to use a context menu for that. 您不必为此使用上下文菜单。 You can use AlertDialog. 您可以使用AlertDialog。 Check out this answer here. 在这里查看此答案。
https://stackoverflow.com/a/45352961/8200290 https://stackoverflow.com/a/45352961/8200290

Just create a different layout with an edit view. 只需使用编辑视图创建其他布局即可。

Here is you solution. 这是您的解决方案。

You are going to have to edit things to make it fit into your application! 您将必须进行编辑以使其适合您的应用程序!

MainActivity class MainActivity类

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);
    }
}

activity_main.xml activity_main.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>

list_view_item.xml list_view_item.xml

<?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.xml custom_alert.xml

-> This will show your EDIT text. ->这将显示您的EDIT文本。 The box at the bottom closes the alert 底部的框关闭了警报

<?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>

@drawable/custom_alert_bg.xml @绘制/ custom_alert_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="#ffffff"/>
    <corners
        android:radius="5dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

You get the following: 您得到以下信息:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM