继第一篇,Android编写学生登录(未用到数据库,账号密码用常量存储),为Listview显示的学生信息每一项长按添加修改和删除功能。

要求:

简单的记录一下,标的是我认为比较有难点的。

(1)增加一个 Activity(ActivityLogin)实现用户登录,ActivityLogin
上包含用户名(编辑框),密码(编辑框),进度条和登录按 钮。 点击登录按钮时,进度条动画开始显示,3 秒之后( 这里需要使用线程来实现延时的效果,这里有个点:每次点击按钮后progress的值会一直增加,所以要再点击监听开始后,给他重置为0) ,验证用户名和 密码(目前设为常量)。验证通过后进入 ActivityMain 界面。
(2) 处理 ActivityMain 界面的 ListView 项长按事件,即长按 ListView 项时,弹出菜单。菜单上有两项功能:编辑和删除。点击编辑时跳转到 ActivityStudent 界面,修改学生信息。点击删除时,删除该条学生信息,删除之前让用户确认一下。


运行效果:

登陆界面:

长按第三项出现修改和删除

 修改3

删除3

 点击取消,不会删除;点击确定,则删除3

 废话不多说了,上代码。

4个Java文件

ActivityLogin.java

package com.example.studentmgr;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityLogin extends Activity {
    String username;
    String password;
    private ProgressBar horizon;
    private int mProgress=0;
    private Handler mHandler;

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

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        horizon=(ProgressBar)findViewById(R.id.progress1);
        horizon.setVisibility(View.GONE);

        final Button button = (Button) findViewById(R.id.login);
        button.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("HandlerLeak")
            @Override
            public void onClick(View view) {
                mProgress=0;
                username = ((EditText) findViewById(R.id.username)).getText().toString();
                password = ((EditText) findViewById(R.id.password)).getText().toString();
                if (!"".equals(username) && !"".equals(password)) {
                    /* 获取进度条*/
                    mHandler = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            horizon.setVisibility(View.VISIBLE);
                            if (msg.what == 0x111) {
                                horizon.setProgress(mProgress);            //更新进度
                            } else {
                                horizon.setVisibility(View.GONE);//设置进度条不显示,并且不占用空间
                                String u1 = "admin";
                                String p1 = "123456";
                                if (u1.equals(username) && p1.equals(password)) {
                                    Intent intent = new Intent();
                                    intent.setClass(ActivityLogin.this, ActivityMain.class);
                                    startActivity(intent);
                                }else{
                                    Toast.makeText(ActivityLogin.this, "您输入的用户信息错误!!!", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    };

                    new Thread(new Runnable() {
                        public void run() {
                            while (true) {//循环获取耗时操作完成的百分比,直到耗时操作结束
                                mProgress += 10;
                                try {
                                    Thread.sleep(300);                    //线程休眠300毫秒
                                } catch (InterruptedException e) {
                                    e.printStackTrace();                //输出异常信息
                                }//获取耗时操作完成的百分比
                                Message m = new Message();            //创建并实例化一个消息对象
                                if (mProgress < 100) {        //当完成进度不到100时表示耗时任务未完成
                                    m.what = 0x111;                    //设置代表耗时操作未完成的消息代码
                                    mHandler.sendMessage(m);        //发送信息
                                } else {                                //当完成进度到达100时表示耗时操作完成
                                    m.what = 0x110;                    //设置代表耗时操作已经完成的消息代码
                                    mHandler.sendMessage(m);        //发送消息
                                    break;                             //退出循环
                                }
                            }
                        }
                    }).start();
                }else {
                    Toast.makeText(ActivityLogin.this, "请填写完整!!!", Toast.LENGTH_SHORT).show();
                }
            }


        });
    }
}

ActivityMain.java

package com.example.studentmgr;
import  com.example.studentmgr.Student;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.lang.String;

public class ActivityMain extends Activity  {
    ArrayList<Student> list1 = new ArrayList<Student>();
    ArrayList<Student> list2 = new ArrayList<Student>();
    ArrayList<Student> list3 = new ArrayList<Student>();
    Student stu1;
    SimpleAdapter adapter;
    private ListView listview;
    public int MID;
    List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();


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

        list1 = (ArrayList<Student>) getIntent().getSerializableExtra("list");
        list2 = (ArrayList<Student>) getIntent().getSerializableExtra("list2");
        if(list1!=null) {
            if(list2!=null){
                list2.addAll(list1);
            }else{
                list2=list1;
            }
            for (int i = 0; i < list2.size(); i++) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("名字", (String) list2.get(i).getName());
                map.put("学号", (String) list2.get(i).getNumber());
                map.put("性别", (String) list2.get(i).getSex());
                map.put("学院", (String) list2.get(i).getCollege());
                map.put("专业", (String) list2.get(i).getSubject());
                map.put("爱好", (String) list2.get(i).getHobby());
                listItems.add(map);
            }
            adapter = new SimpleAdapter(this, listItems, R.layout.item, new String[]{"名字", "学号", "性别", "学院", "专业", "爱好"}, new int[]{R.id.name, R.id.number, R.id.sex, R.id.college, R.id.subject, R.id.checked});// 创建SimpleAdapter
            listview = (ListView) findViewById(R.id.listview); // 获取列表视图
            listview.setAdapter(adapter);// 将适配器与ListView关联
            ItemOnLongClick();
        }
        
        Button button = (Button) findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent();
                intent1.setClass(ActivityMain.this, ActivityStudent.class);
                intent1.putExtra("list2", (Serializable) list2);
                startActivity(intent1);
            }
        });


    }

    private void ItemOnLongClick() {
//注:setOnCreateContextMenuListener是与下面onContextItemSelected配套使用的
        listview.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                menu.add(0, 0, 0, "修改");
                menu.add(0, 1, 0, "删除");
            }


        });

    }
    
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuinfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        Intent intent1 = new Intent();
        final int position=(int) listview.getAdapter().getItemId(menuinfo.position);

        switch (item.getItemId()) {
            case 0:
                //  修改操作
                int s=0;
                intent1.putExtra("list2", (Serializable) list2);
                intent1.putExtra("s",s);
                intent1.putExtra("position",position);
                intent1.setClass(ActivityMain.this, ActivityStudent.class);
                startActivity(intent1);
                adapter.notifyDataSetChanged();
                Toast.makeText(ActivityMain.this, "修改", Toast.LENGTH_SHORT).show();
                break;

            case 1:
                AlertDialog.Builder builder=new AlertDialog.Builder(ActivityMain.this);
                builder.setMessage("确定删除"+list2.get(position).getName()+"?");
                builder.setTitle("提示");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(listItems.remove(position)!=null){
                            System.out.println("success");
                        }else {
                            System.out.println("failed");
                        }
                        adapter.notifyDataSetChanged();
                        list2.remove(position);
                        Toast.makeText(getBaseContext(), "删除列表项", Toast.LENGTH_SHORT).show();
                    }
                });

                //添加AlertDialog.Builder对象的setNegativeButton()方法
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create().show();
                break;
            default:
                break;
        }

        return super.onContextItemSelected(item);

    }
}

ActivityStudent.java

package com.example.studentmgr;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ActivityStudent extends Activity {
    RadioButton s1, s2;
    Student student=new Student();
    ArrayList<Student> list = new ArrayList<Student>();




    Spinner spcollege, spsubject;
    ArrayAdapter<String> collegeAdapter;
    ArrayAdapter<String> subjectAdapter;
    String name = "";
    String number = "";
    String sex = "";
    String college = "";
    String subject = "";
    String checked = "";
    String[] collegeList = new String[]{"计算机学院", "电气学院"};
    String[] jisuanji = new String[]{"软件工程", "信息安全"};
    String[] dianqi = new String[]{"电气工程", "电机工程"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student);
        final ArrayList<Student> list2 = (ArrayList<Student>) getIntent().getSerializableExtra("list2");
        final int s=(int)getIntent().getIntExtra("s",-1);
        final int mid=(int)getIntent().getIntExtra("position",-1);
        if (s==0) {
            TextView name=(TextView) findViewById(R.id.name);
            name.setText(list2.get(mid).getName());
            TextView number=(TextView) findViewById(R.id.number);
            number.setText(list2.get(mid).getNumber());
        }



            s1 = (RadioButton) findViewById(R.id.man);
            s2 = (RadioButton) findViewById(R.id.woman);

            spcollege = (Spinner) findViewById(R.id.spcollege);
            spsubject = (Spinner) findViewById(R.id.spsubject);
            setSpinner();

            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            final CheckBox ID1 = (CheckBox) findViewById(R.id.ID1);
            final CheckBox ID2 = (CheckBox) findViewById(R.id.ID2);
            final CheckBox ID3 = (CheckBox) findViewById(R.id.ID3);
            final CheckBox ID4 = (CheckBox) findViewById(R.id.ID4);


            final Button button = (Button) findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    name = ((EditText) findViewById(R.id.name)).getText().toString();

                    number = ((EditText) findViewById(R.id.number)).getText().toString();
                    if (s1.isChecked()) {
                        sex = ((RadioButton) findViewById(R.id.man)).getText().toString();
                    } else {
                        sex = ((RadioButton) findViewById(R.id.woman)).getText().toString();
                    }


                    if (ID1.isChecked()) {
                        checked += ID1.getText().toString();
                    }
                    if (ID2.isChecked()) {
                        checked += ID2.getText().toString();
                    }
                    if (ID3.isChecked()) {
                        checked += ID3.getText().toString();
                    }
                    if (ID4.isChecked()) {
                        checked += ID4.getText().toString();
                    }

                    student.setName(name);
                    student.setNumber(number);
                    student.setSex(sex);
                    student.setCollege(college);
                    student.setSubject(subject);
                    student.setHobby(checked);
                    list.add(student);




                    if (s==0) {
                        TextView name=(TextView) findViewById(R.id.name);
                        name.setText(list2.get(mid).getName());
                        list2.set(mid, student);
                        Intent intent2 = new Intent(ActivityStudent.this, ActivityMain.class);
                        intent2.putExtra("list", (Serializable) list2);
                        startActivity(intent2);
                    } else if(s!=0){

                        if (!"".equals(name) && !"".equals(number)) {
                            Intent intent = new Intent(ActivityStudent.this, ActivityMain.class);
                            intent.putExtra("list", (Serializable) list);
                            if (list2 != null) {
                                intent.putExtra("list2", (Serializable) list2);
                            }
                            startActivity(intent);
                        } else {
                            Toast.makeText(ActivityStudent.this, "请填写完整!!!", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });

    }

    public void setSpinner() {
        //绑定适配器和值
        collegeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, collegeList);
        spcollege.setAdapter(collegeAdapter);
        spcollege.setSelection(0);//设置初始默认值
        //绑定适配器和值
        subjectAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, jisuanji);
        spsubject.setAdapter(subjectAdapter);
        spsubject.setSelection(0);//设置初始默认值

        //设置列表项选中监听
        spcollege.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                //获取选中项的值
                college = adapterView.getItemAtPosition(i).toString();
                //根据选中的不同的值绑定不同的适配器
                if (college.equals("计算机学院")) {
                    subjectAdapter = new ArrayAdapter<String>(ActivityStudent.this, android.R.layout.simple_spinner_item,jisuanji);
                    spsubject.setAdapter(subjectAdapter);
                } else if (college.equals("电气学院")) {
                    subjectAdapter = new ArrayAdapter<String>(ActivityStudent.this, android.R.layout.simple_spinner_item,dianqi);
                    spsubject.setAdapter(subjectAdapter);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });


        //设置列表项选中监听
        spsubject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                //获取列表项的值
                subject = adapterView.getItemAtPosition(i).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }
}

还有一个Student.java在前一篇 没有修改,链接Android 编写一个若干学生信息录入页面 /学生信息显示与添加,使用Listview输出学生信息。_编程虐我千百遍,我代编程如初恋的博客-CSDN博客

4个xml文件,其中另外三个在前一篇,链接同上

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  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"
    android:orientation="vertical"
    tools:context="com.example.studentmgr.ActivityLogin">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:gravity="center">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/text1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_above="@+id/password"
            android:text="账号:"
            android:textSize="25dp" />
        <EditText
            android:id="@+id/username"
            android:layout_toRightOf="@+id/text1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:hint="请输入账号"
            android:inputType="textPersonName" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/text2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_alignBottom="@+id/password"
            android:text="密码:"
            android:textSize="25dp" />

        <EditText
            android:id="@+id/password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:hint="请输入密码"
            android:inputType="textPassword" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>


    </LinearLayout>



        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="登录" />



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <ProgressBar
            android:id="@+id/progress1"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="60dp"
            android:max="100"/>

    </LinearLayout>
</LinearLayout>
  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值