标签:管理系统
1,创建主界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/tv_system" android:textSize="20sp" android:layout_marginTop="10dip" android:gravity="center" android:background="#CFCFCF" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/system" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_name" android:textSize="18sp" android:layout_marginTop="10dip" android:layout_marginLeft="5dip" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" android:text="@string/name"/> <TextView android:id="@+id/tv_sex" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:layout_weight="1" android:text="@string/sex" android:textSize="18sp" /> <TextView android:id="@+id/tv_age" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:layout_weight="1" android:text="@string/age" android:textSize="18sp" /> <TextView android:id="@+id/tv_add" android:textSize="18sp" android:layout_width="0dip" android:layout_weight="1" android:layout_marginTop="10dip" android:layout_height="wrap_content" android:text="@string/add"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/et_name" android:layout_width="0dip" android:layout_weight="1" android:layout_marginLeft="5dip" android:layout_gravity="center" android:layout_height="wrap_content"/> <EditText android:id="@+id/et_sex" android:layout_width="0dip" android:layout_weight="1" android:layout_gravity="center" android:layout_height="wrap_content"/> <EditText android:id="@+id/et_age" android:layout_width="0dip" android:layout_weight="1" android:layout_gravity="center" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_add" android:textSize="18sp" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="@string/addStudent"/> </LinearLayout> <ListView android:id="@+id/lv_values" android:layout_width="fill_parent" android:layout_height="200dip" android:layout_marginLeft="5dip"> </ListView> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:id="@+id/btn_save" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/save"/> <Button android:id="@+id/btn_restore" android:textSize="20sp" android:layout_marginLeft="20dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/restore"/> </LinearLayout> </LinearLayout> |
2,创建listView高级控件中的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/tv_listName" android:textSize="20sp" android:layout_marginLeft="5dip" android:layout_marginTop="10dip" android:textColor="@android:color/black" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_listSex" android:textSize="20sp" android:layout_marginTop="10dip" android:textColor="@android:color/black" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_listAge" android:textSize="20sp" android:layout_marginTop="10dip" android:textColor="@android:color/black" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout> |
3,封装一个学生类
package com.eduask.studentmanagersystem; public class Students { private String name; private String sex; private int age; public Students(){} public Students(String name, String sex, int age) { this.name = name; this.sex = sex; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
4,具体的实现
package com.eduask.studentmanagersystem; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private List<Students> students = new ArrayList<Students>(); private ListView lv_values; private Button btn_add; private MyAdapter adapter; private EditText et_name; private EditText et_sex; private EditText et_age; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找到相应的控件 lv_values = (ListView) findViewById(R.id.lv_values); btn_add = (Button) findViewById(R.id.btn_add); et_name = (EditText) findViewById(R.id.et_name); et_sex = (EditText) findViewById(R.id.et_sex); et_age = (EditText) findViewById(R.id.et_age); //自定义适配器 adapter = new MyAdapter(); if(adapter != null){ lv_values.setAdapter(adapter); } btn_add.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String name = et_name.getText().toString().trim(); String sex = et_sex.getText().toString().trim(); int age = Integer.parseInt(et_age.getText().toString().trim()); if(name != null && sex != null && age != 0){ Students stu = new Students(); stu.setName(name); stu.setSex(sex); stu.setAge(age); students.add(stu); Toast.makeText(getApplicationContext(), "增加成功!!", 2000).show(); adapter.notifyDataSetChanged(); //更新 } } }); } private class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return students.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { //加载布局 View contentView = View.inflate(getApplicationContext(), R.layout.list_students, null); //找到布局中的控件 TextView tv_listName = (TextView) contentView.findViewById(R.id.tv_listName); TextView tv_listSex = (TextView) contentView.findViewById(R.id.tv_listSex); TextView tv_listAge = (TextView) contentView.findViewById(R.id.tv_listAge); //加载相应的数据 tv_listName.setText(students.get(position).getName()); tv_listSex.setText(students.get(position).getSex()); tv_listAge.setText(String.valueOf(students.get(position).getAge())); return contentView; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } } } |
本文出自 “InProvence” 博客,谢绝转载!
标签:管理系统
原文地址:http://9882931.blog.51cto.com/9872931/1610344