实验3布局:
<?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=".MainActivity"
android:background="@color/layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="班级:"
android:textSize="20sp" />
<EditText
android:id="@+id/et_class"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号:"
android:textSize="20sp"/>
<EditText
android:id="@+id/et_number"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="20sp"/>
<EditText
android:id="@+id/et_name"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_button"
android:textSize="20sp"
android:background="@color/app_bk"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="班级"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="学号"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="姓名"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
</LinearLayout>
实验3实现:
package com.example.walker.exp3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
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.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private Button btn_add;
private EditText et_name;
private EditText et_class;
private EditText et_numer;
private ListView listview;
private SimpleAdapter simpleAdapter;
private List<Map<String, Object>> Datas = new ArrayList<Map<String, Object>>();
String number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化数据集
listview = (ListView) findViewById(R.id.listview);
et_class = (EditText) findViewById(R.id.et_class);
et_name = (EditText) findViewById(R.id.et_name);
et_numer = (EditText) findViewById(R.id.et_number);
btn_add = (Button) findViewById(R.id.btn_add);
//为 ListView 的所有 item 注册 ContextMenu
this.registerForContextMenu(listview);
//实例化SimpleAdapter
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("class", "计算机162");
map1.put("number", "2016012841");
map1.put("name", "沈仕瑞");
Datas.add(map1);
//设置创建上下文菜单的监听
//listview.setOnCreateContextMenuListener(this);
设置按钮的监听
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et_class.getText().toString().trim().equals("") || et_numer.getText().toString().trim().equals("") || et_name.getText().toString().trim().equals("")) {
Toast.makeText(MainActivity.this, "请输入正确数据", Toast.LENGTH_SHORT).show();
} else {
Map<String, Object> item = new HashMap<String, Object>();
item.put("class", et_class.getText().toString());
item.put("number", et_numer.getText().toString());
item.put("name", et_name.getText().toString());
Datas.add(item);
simpleAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "添加数据成功", Toast.LENGTH_SHORT).show();
}
}
});
//map对象中的key的数组, 用于得到对应的value
String[] from = {"class", "number", "name"};
//Item布局文件中的子view的id的数组
int[] to = {R.id.tv_class, R.id.tv_number, R.id.tv_name};
// /设置适配器
simpleAdapter = new SimpleAdapter(this, Datas, R.layout.item_view, from, to);
listview.setAdapter(simpleAdapter);
}
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//添加菜单项
menu.add(0, 1, 0, "删除");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int pos = (int) listview.getAdapter().getItemId(menuInfo.position);
if (Datas.remove(pos) != null) {
System.out.println("success");
} else {
System.out.println("failed");
}
simpleAdapter.notifyDataSetChanged();
Toast.makeText(getBaseContext(), "删除此项", Toast.LENGTH_SHORT).show();
return super.onContextItemSelected(item);
}
}
实验5布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="horizontal">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="秒 表"
android:textAlignment="center"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="74dp"
android:orientation="horizontal">
<TextView
android:id="@+id/hour"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center" />
<TextView
android:id="@+id/min"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center" />
<TextView
android:id="@+id/sec"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="64dp"
android:orientation="horizontal">
<Button
android:id="@+id/ret0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="清 零" />
<Button
android:id="@+id/ready"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="计 时" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="停 止" />
</LinearLayout>
</LinearLayout>
实验5实现:
package com.example.administrator.sy5;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
private TextView Hour;
private TextView Min;
private TextView Sec;
private Button Stop;
private Button Ready;
private Button Ret0;
private boolean isPaused = false;
private String timeUsed;
private int timeUsedInsec;
private Handler uiHandle = new Handler(){
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case 1:
if(!isPaused) {
addTimeUsed();
updateClockUI();
}
uiHandle.sendEmptyMessageDelayed(1, 1000);
break;
default: break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Hour = (TextView)findViewById(R.id.hour);
Min = (TextView)findViewById(R.id.min);
Sec = (TextView)findViewById(R.id.sec);
Ret0 = (Button)findViewById(R.id.ret0);
Ready = (Button)findViewById(R.id.ready);
Stop = (Button)findViewById(R.id.stop);
Ready.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
uiHandle.removeMessages(1);
startTime();
isPaused = false;
}
});
Stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
isPaused = true;
timeUsedInsec = 0;
}
});
Ret0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
isPaused = true;
timeUsedInsec = 0;
Hour.setText("0"+":");
Min.setText("0" + ":");
Sec.setText("0");
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
isPaused = true;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
isPaused = false;
}
private void startTime(){
uiHandle.sendEmptyMessageDelayed(1, 1000);
}
private void updateClockUI(){
Hour.setText(getHour()+":");
Min.setText(getMin() + ":");
Sec.setText(getSec());
}
public void addTimeUsed(){
timeUsedInsec = timeUsedInsec + 1;
timeUsed =this.getHour()+":"+ this.getMin() + ":" + this.getSec();
}
public CharSequence getHour(){
int min = timeUsedInsec / 60;
return String.valueOf(min / 60);
}
public CharSequence getMin(){
return String.valueOf(timeUsedInsec / 60);
}
public CharSequence getSec(){
int sec = timeUsedInsec % 60;
return sec < 10? "0" + sec :String.valueOf(sec);
}
}
<?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=".MainActivity"
android:background="@color/layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="班级:"
android:textSize="20sp" />
<EditText
android:id="@+id/et_class"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号:"
android:textSize="20sp"/>
<EditText
android:id="@+id/et_number"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="20sp"/>
<EditText
android:id="@+id/et_name"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_button"
android:textSize="20sp"
android:background="@color/app_bk"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="班级"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="学号"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="姓名"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
</LinearLayout>
实验3实现:
package com.example.walker.exp3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
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.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private Button btn_add;
private EditText et_name;
private EditText et_class;
private EditText et_numer;
private ListView listview;
private SimpleAdapter simpleAdapter;
private List<Map<String, Object>> Datas = new ArrayList<Map<String, Object>>();
String number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化数据集
listview = (ListView) findViewById(R.id.listview);
et_class = (EditText) findViewById(R.id.et_class);
et_name = (EditText) findViewById(R.id.et_name);
et_numer = (EditText) findViewById(R.id.et_number);
btn_add = (Button) findViewById(R.id.btn_add);
//为 ListView 的所有 item 注册 ContextMenu
this.registerForContextMenu(listview);
//实例化SimpleAdapter
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("class", "计算机162");
map1.put("number", "2016012841");
map1.put("name", "沈仕瑞");
Datas.add(map1);
//设置创建上下文菜单的监听
//listview.setOnCreateContextMenuListener(this);
设置按钮的监听
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et_class.getText().toString().trim().equals("") || et_numer.getText().toString().trim().equals("") || et_name.getText().toString().trim().equals("")) {
Toast.makeText(MainActivity.this, "请输入正确数据", Toast.LENGTH_SHORT).show();
} else {
Map<String, Object> item = new HashMap<String, Object>();
item.put("class", et_class.getText().toString());
item.put("number", et_numer.getText().toString());
item.put("name", et_name.getText().toString());
Datas.add(item);
simpleAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "添加数据成功", Toast.LENGTH_SHORT).show();
}
}
});
//map对象中的key的数组, 用于得到对应的value
String[] from = {"class", "number", "name"};
//Item布局文件中的子view的id的数组
int[] to = {R.id.tv_class, R.id.tv_number, R.id.tv_name};
// /设置适配器
simpleAdapter = new SimpleAdapter(this, Datas, R.layout.item_view, from, to);
listview.setAdapter(simpleAdapter);
}
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//添加菜单项
menu.add(0, 1, 0, "删除");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int pos = (int) listview.getAdapter().getItemId(menuInfo.position);
if (Datas.remove(pos) != null) {
System.out.println("success");
} else {
System.out.println("failed");
}
simpleAdapter.notifyDataSetChanged();
Toast.makeText(getBaseContext(), "删除此项", Toast.LENGTH_SHORT).show();
return super.onContextItemSelected(item);
}
}
实验5布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="horizontal">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="秒 表"
android:textAlignment="center"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="74dp"
android:orientation="horizontal">
<TextView
android:id="@+id/hour"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center" />
<TextView
android:id="@+id/min"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center" />
<TextView
android:id="@+id/sec"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="64dp"
android:orientation="horizontal">
<Button
android:id="@+id/ret0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="清 零" />
<Button
android:id="@+id/ready"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="计 时" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="停 止" />
</LinearLayout>
</LinearLayout>
实验5实现:
package com.example.administrator.sy5;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
private TextView Hour;
private TextView Min;
private TextView Sec;
private Button Stop;
private Button Ready;
private Button Ret0;
private boolean isPaused = false;
private String timeUsed;
private int timeUsedInsec;
private Handler uiHandle = new Handler(){
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case 1:
if(!isPaused) {
addTimeUsed();
updateClockUI();
}
uiHandle.sendEmptyMessageDelayed(1, 1000);
break;
default: break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Hour = (TextView)findViewById(R.id.hour);
Min = (TextView)findViewById(R.id.min);
Sec = (TextView)findViewById(R.id.sec);
Ret0 = (Button)findViewById(R.id.ret0);
Ready = (Button)findViewById(R.id.ready);
Stop = (Button)findViewById(R.id.stop);
Ready.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
uiHandle.removeMessages(1);
startTime();
isPaused = false;
}
});
Stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
isPaused = true;
timeUsedInsec = 0;
}
});
Ret0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
isPaused = true;
timeUsedInsec = 0;
Hour.setText("0"+":");
Min.setText("0" + ":");
Sec.setText("0");
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
isPaused = true;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
isPaused = false;
}
private void startTime(){
uiHandle.sendEmptyMessageDelayed(1, 1000);
}
private void updateClockUI(){
Hour.setText(getHour()+":");
Min.setText(getMin() + ":");
Sec.setText(getSec());
}
public void addTimeUsed(){
timeUsedInsec = timeUsedInsec + 1;
timeUsed =this.getHour()+":"+ this.getMin() + ":" + this.getSec();
}
public CharSequence getHour(){
int min = timeUsedInsec / 60;
return String.valueOf(min / 60);
}
public CharSequence getMin(){
return String.valueOf(timeUsedInsec / 60);
}
public CharSequence getSec(){
int sec = timeUsedInsec % 60;
return sec < 10? "0" + sec :String.valueOf(sec);
}
}