当前位置:主页 > 移动开发 > Android代码 >

android studio实验: UI设计 ListView及事件响应

时间:2022-09-28 09:40:01 | 栏目:Android代码 | 点击:

实验目的:

实验要求:

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();//防止把第一行给删了
    }

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

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

您可能感兴趣的文章:

相关文章