android studio实验 UI设计 ListView及其事件响应方法 弹出菜单PopupMenu及其事件响应方法
Published on 2022-11-17 23:04 in 暂未分类 with 林动

android studio实验 UI设计 ListView及其事件响应方法 弹出菜单PopupMenu及其事件响应方法

    实验目的:
    1.学习使用ListView及其事件响应方法。
    2.阅读sdk/doc/index脱机文档或者在Android developer开发者网站上,学习使用弹出菜单PopupMenu及其事件响应方法。
    实验要求:
    1.实现一个列表,其中显示班级学号姓名,提供添加功能
    2.对添加的选项,通过长按某项,调用弹出菜单显示删除功能,并删除该选项。

    在这里插入图片描述
    在这里插入图片描述

    MainActivity.java

    package com.example.shiyan3;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.appcompat.widget.PopupMenu;
    
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.view.ContextMenu;
    import android.view.Gravity;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
    
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;
    
    
    
    
    public class MainActivity extends AppCompatActivity {
    
    
        List<String> list;
    
        ArrayAdapter<String> adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            EditText t1=(EditText) findViewById(R.id.edit_1);
            EditText t2=(EditText) findViewById(R.id.edit_2);
            EditText t3=(EditText) findViewById(R.id.edit_3);
            ListView listView = (ListView)findViewById(R.id.listview);
            Button bt= (Button) findViewById(R.id.button);
    
            list = new ArrayList<String>();
            list.add("班级                     姓名                        学号");
    
            adapter=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,list);
            listView.setAdapter(adapter);
    
            bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    list.add(t1.getText().toString()+"              "+t2.getText().toString()+"              "+t3.getText().toString());
                    adapter.notifyDataSetChanged();
                }
            });
    
            AdapterView.OnItemLongClickListener lclistener=new AdapterView.OnItemLongClickListener()
            {
                @Override
                public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                    popupshow(view,i);
                    adapter.notifyDataSetChanged();
                    return false;
                }
            };
            listView.setOnItemLongClickListener(lclistener);
    
        }
    
    
        public  void popupshow(View v,int i){//自定义函数
            PopupMenu popup=new PopupMenu(this,v);
            popup.setGravity(Gravity.CENTER_HORIZONTAL);
            MenuInflater inflater=popup.getMenuInflater();
            inflater.inflate(R.menu.popupmenu,popup.getMenu());
    
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    if(item.getItemId()==R.id.delete)
                    {
                        list.remove(i);
                        adapter.notifyDataSetChanged();
                    }
                    return false;
                }
            });
    
            if(i!=0) popup.show();//防止把第一行给删了
        }
    
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <TextView
                android:id="@+id/textview1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
    
                android:text="班级:"
                android:textSize="20sp" />
            <EditText
                android:id="@+id/edit_1"
                android:layout_height="wrap_content"
                android:layout_width="500px"
    
                />
    
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <TextView
                android:id="@+id/textview2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="姓名:"
                android:textSize="20sp" />
    
            <EditText
                android:id="@+id/edit_2"
                android:layout_width="500px"
                android:layout_height="wrap_content"
                android:text="" />
    
        </TableRow>
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <TextView
                android:id="@+id/textview3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="学号:"
                android:textSize="20sp" />
    
            <EditText
                android:id="@+id/edit_3"
                android:layout_width="500px"
                android:layout_height="wrap_content"
                android:text="" />
    
        </TableRow>
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="350px"
            android:layout_marginRight="350px"
            android:text="添加" />
    
        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
        </ListView>
    
    </TableLayout>
    

    popupmenu.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:id="@+id/delete"
            android:title="删除"/>
       <item
            android:id="@+id/add"
        android:title="添加"/>
    
    </menu>
    
    posted @   林动  阅读(78)  评论(0编辑  收藏  举报
    相关博文:
    阅读排行:
    · winform 绘制太阳,地球,月球 运作规律
    · 震惊!C++程序真的从main开始吗?99%的程序员都答错了
    · AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
    · 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
    · 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
    点击右上角即可分享
    微信分享提示