.

目录

​实验二​

​注意点:​

​实现​

​麻烦但实时的写法​

​简易写法:(可以不选)​

​小结:​

​实验三​

​activity_main.xml​

​item.xml​

​MainActivity.java​

​实验四菜单和对话框​

​1、上下文菜单​

​activity_main.xml​

​MainActivity.java​

​2、日期选择对话框(DatePickerDialog)​


​activity_main.xml2​

​MainActivity.java2​

​3、多项选择对话框​

​activity_main.xml3​

​MainActivity.java3​

​实验五 Activity和Intent​

​1.两个Activity传递数据​

​activity_main.xml 5_1​

​activity_result.xml 5_1​

​MainActivity.java 5_1​

​ResultActivity.java​

​2.Intent实现一个电话拨号程序​

​activity_main.xml 5_2​

​MainActivity.java 5_2​

​实验七 文件操作(listview)​

​listview学生记录​


​activity_main.xml 7​

​item.xml​

​add.xml​

​MainActivity.java 7​

​StudentInfoUtils.java ​

​模拟试题​

​注册用户,写入文件,到另一个activity上再读出来​

​activity_main.xml 模拟​

​activity_result.xml 模拟​

​AndroidManifest.xml​

​MainActivity.java 模拟​

​ResultActivity.java 模拟​

​StudentInfoUtils.java 模拟​


实验二

androd备考 平时实验_java

注意点:

滚动布局

ScrollView

androd备考 平时实验_xml_02

滚动布局

新建的模板:

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> </ScrollView>

加上几行添加周围空白

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > </LinearLayout> </ScrollView>

 

注意:hobbys=hobbys.replaceAll(hobby, "");   //一定要重新赋值回给hobbys 该api不会修改原来的hobbys

StringBuffer 的append在此处比string好用多了  还会自动转换

实现

麻烦但实时的写法

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />

<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true"
android:hint="请输入口令" />

<TextView
android:id="@+id/tv_major"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="专业"
android:textSize="30sp" />

<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="软件" />

<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="物联网" />

<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算机" />

<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="信管" />
</RadioGroup>

<TextView
android:id="@+id/tv_hobby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="爱好"
android:textSize="30sp" />

<CheckBox
android:id="@+id/cb_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上网" />


<CheckBox
android:id="@+id/cb_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="运动" />

<CheckBox
android:id="@+id/cb_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读书" />

<TextView
android:id="@+id/tv_hobby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="出生年月"
android:textSize="30sp" />

<DatePicker
android:id="@+id/dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:maxDate="12-31-2940"
android:minDate="1-1-1970"
android:spinnersShown="true" />

<Button
android:id="@+id/dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="提交" />

</LinearLayout>

</ScrollView>

MainActivity.java

package com.example.glkexp02;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity implements OnCheckedChangeListener,android.widget.RadioGroup.OnCheckedChangeListener{

private EditText et_name;
private EditText et_pwd;
private RadioGroup rg;
private CheckBox cb1;
private CheckBox cb2;
private CheckBox cb3;
private DatePicker dp;

private String major="";
private String hobbys="";
private String birth="";

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

et_name = (EditText) findViewById(R.id.et_name);
et_pwd = (EditText) findViewById(R.id.et_pwd);
rg = (RadioGroup) findViewById(R.id.rg);
cb1 = (CheckBox) findViewById(R.id.cb_1);
cb2 = (CheckBox) findViewById(R.id.cb_2);
cb3 = (CheckBox) findViewById(R.id.cb_3);
dp = (DatePicker) findViewById(R.id.dp);

rg.setOnCheckedChangeListener(this);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
}

public void click(View v){
String name = et_name.getText().toString();
String pwd = et_pwd.getText().toString();

if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
Toast.makeText(this, "用户名 或 密码 不能为空!", 0).show();
return;
}
if(TextUtils.isEmpty(major)){
Toast.makeText(this, "请选择专业!", 0).show();
return;
}
if(TextUtils.isEmpty(hobbys)){
Toast.makeText(this, "请选择爱好!", 0).show();
return;
}

birth=dp.getYear()+" "+(dp.getMonth()+1)+" "+dp.getDayOfMonth();
String info="\n用 户 名:"+name+
"\n密 码:"+pwd+
"\n专 业:"+major+
"\n爱 好:"+hobbys+
"\n出生年月:"+birth
;

Toast.makeText(this, info, 1).show();
}

//选择专业
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb_check = (RadioButton) findViewById(checkedId);
major=rb_check.getText().toString();

}

//选择爱好
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String hobby = " "+buttonView.getText().toString();
if(isChecked){
hobbys+=hobby;
}else{
hobbys=hobbys.replaceAll(hobby, "");//删除此hobby 一定记得赋值回给hobbys
//Toast.makeText(this, "删除"+hobby+"\n删除后:"+hobbys, 1).show();
}
}
}

简易写法:(可以不选)

package com.example.glkexp02;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void click(View v){

EditText et_name = (EditText) findViewById(R.id.et_name);
EditText et_pwd = (EditText) findViewById(R.id.et_pwd);
RadioGroup rg = (RadioGroup) findViewById(R.id.rg);
RadioButton rd_check = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
CheckBox cb1 = (CheckBox) findViewById(R.id.cb_1);
CheckBox cb2 = (CheckBox) findViewById(R.id.cb_2);
CheckBox cb3 = (CheckBox) findViewById(R.id.cb_3);
DatePicker dp = (DatePicker) findViewById(R.id.dp);

StringBuffer info=new StringBuffer();
info.append("\n姓 名:"+et_name.getText());
info.append("\n密 码:"+et_pwd.getText());
if(rd_check!=null) info.append("\n专 业:"+rd_check.getText());
info.append("\n爱 好:");
if(cb1.isChecked()) info.append(" "+cb1.getText());
if(cb2.isChecked()) info.append(" "+cb2.getText());
if(cb3.isChecked()) info.append(" "+cb3.getText());
info.append("\n出生日期:"+dp.getYear()+"年"+(dp.getMonth()+1)+"月"+dp.getDayOfMonth()+"日");

Toast.makeText(this, info+"\n", 1).show();
}
}

 

androd备考 平时实验_xml_03

 

小结:

界面布局时采用拖拽+修改的方式最为快速,建议采用

 

实验三

androd备考 平时实验_android_04

activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>

</RelativeLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image"
android:orientation="vertical" >

<TextView
android:id="@+id/tv_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="学号:" />

<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="姓名:" />

<TextView
android:id="@+id/tv_tel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="电话:" />
</LinearLayout>

</RelativeLayout>

MainActivity.java

package com.example.glkexp03;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener{

List<Map<String, Object>> list;
//图片id
private int[] imageId=new int[]{
R.drawable.lovegirl,R.drawable.mouse,R.drawable.pkq,R.drawable.yizh
};
//学号
private String[] no=new String[]{
"3100112110","3100112111","3100112112","3100112113"
};
//姓名
private String[] name=new String[]{
"lovegirl","mouse","piKaQiu","coolBoy"
};
//电话
private String[] tel=new String[]{
"17855396541","17855397412","178553698521","17855274156",
};


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

setTitle("卡通通讯录");

ListView lv = (ListView) findViewById(R.id.lv);
list=new ArrayList<Map<String,Object>>();
for (int i = 0; i < name.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", imageId[i]);
map.put("no", "学号:"+no[i]);
map.put("name", "姓名:"+name[i]);
map.put("tel", "电话:"+tel[i]);
list.add(map);
}


SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item,
new String[]{"image","no","name","tel"},
new int[]{R.id.image,R.id.tv_no,R.id.tv_name,R.id.tv_tel});
//设置适配器
lv.setAdapter(adapter);
//设置点击事件
lv.setOnItemClickListener(this);

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(this, "您点击的是:"+name[position], 0).show();
}
}

 

实验四菜单和对话框

1、上下文菜单

注意上下文菜单长按才触发事件

androd备考 平时实验_java_05

activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:textSize="30sp"
android:textColor="#F00"
android:text="我是变色龙" />

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:textSize="30sp"
android:text="变色" />

</RelativeLayout>

MainActivity.java

package com.example.glkexp04;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private Button button;
private TextView tv_info;

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

tv_info = (TextView) findViewById(R.id.textView1);
button = (Button) findViewById(R.id.button1);

registerForContextMenu(button);//注意上下文菜单需要长按才会起作用

}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v == button) {
// menu.add(groupId, itemId, order, title)
menu.add(200, 200, 200, "红色");
menu.add(201, 201, 201, "绿色");
menu.add(202, 202, 202, "蓝色");
}

}

@Override
public boolean onContextItemSelected(MenuItem item) {
String option = item.getTitle().toString();
if (option.equals("红色")) {
tv_info.setTextColor(Color.RED);//注意Color.RED这种写法很重要
} else if (option.equals("绿色")) {
tv_info.setTextColor(Color.GREEN);
} else if (option.equals("蓝色")) {
tv_info.setTextColor(Color.BLUE);
}
return super.onContextItemSelected(item);
}

}

2、日期选择对话框(DatePickerDialog)

androd备考 平时实验_android_06

activity_main.xml2

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="日期" />

<EditText
android:id="@+id/et_date"
android:layout_toRightOf="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="点击此处输入日期"
/>

</RelativeLayout>

MainActivity.java2

package com.example.glkexp04_2;

import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener,OnDateSetListener{

private EditText et_date;

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

et_date = (EditText) findViewById(R.id.et_date);

et_date.setOnClickListener(this);
}

@SuppressLint("NewApi") @Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
//千万注意 第二个参数不能写null了 他就是设置选择时的触发事件的 OnDateSetListener
DatePickerDialog dialog = new DatePickerDialog(this, this,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
dialog.show();
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
et_date.setText(year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日");
}


}

3、多项选择对话框

androd备考 平时实验_xml_07

activity_main.xml3

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好"
android:textSize="30sp" />

<EditText
android:id="@+id/et_hobby"
android:onClick="clickET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv"
android:hint="点击此处选择爱好" />

</RelativeLayout>

MainActivity.java3

package com.example.glkexp04_3;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity implements
OnMultiChoiceClickListener {

String[] options = new String[] { "火车", "飞机", "轮船" };
private EditText hobby;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hobby = (EditText) findViewById(R.id.et_hobby);

}

public void clickET(View v) {
Builder builder = new AlertDialog.Builder(this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("请选择爱好");
builder.setPositiveButton("确定", null);
// builder.setMultiChoiceItems(items, checkedItems, listener)
//保留之前选中的
boolean[] checkedItems = new boolean[options.length];
for (int i = 0; i < options.length; i++) {
if (hobby.getText().toString().contains(options[i])) {
checkedItems[i] = true;
} else {
checkedItems[i] = false;
}
}
builder.setMultiChoiceItems(options, checkedItems, this).show();
}

// 选中爱好时触发
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
String old = hobby.getText().toString();
if (isChecked) {
if (!old.contains(" " + options[which])) {
hobby.append(" " + options[which]);
}
} else {
String now = old.replace(" " + options[which], "");
hobby.setText(now);
}
}

}

 

实验五 Activity和Intent

1.两个Activity传递数据

androd备考 平时实验_android_08

androd备考 平时实验_java_09

androd备考 平时实验_xml_10

activity_main.xml 5_1

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
android:id="@+id/et_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
/>

<TextView
android:id="@+id/tv_backRecive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/et_send"
android:layout_below="@+id/et_send"
android:text="回传数据为空"
android:textSize="30sp"
/>

<Button
android:id="@+id/button1"
android:onClick="clickSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_backRecive"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="发送" />

</RelativeLayout>

activity_result.xml 5_1

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
android:id="@+id/et_backmsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
/>

<TextView
android:id="@+id/tv_receive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/et_backmsg"
android:textSize="30sp"
android:text="接收数据为空" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/tv_receive"
android:layout_below="@+id/tv_receive"
android:layout_marginRight="14dp"
android:layout_marginTop="20dp"
android:onClick="clickBack"
android:text="返回" />

</RelativeLayout>

MainActivity.java 5_1

package com.example.glkexp05_1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

private EditText et_send;
private TextView back;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_send = (EditText) findViewById(R.id.et_send);
back = (TextView) findViewById(R.id.tv_backRecive);
}

public void clickSend(View v){
String sendMsg = et_send.getText().toString();
Intent intent = new Intent();
intent.setClass(this, ResultActivity.class);
intent.putExtra("send", sendMsg);
startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case RESULT_OK:
String str=data.getStringExtra("back");
back.setText("回传数据为:"+str);
break;
default:
break;
}
}

}

ResultActivity.java

package com.example.glkexp05_1;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class ResultActivity extends MainActivity {
private Intent intent;
private TextView tv_receive;
private EditText back;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
tv_receive = (TextView) findViewById(R.id.tv_receive);
back = (EditText) findViewById(R.id.et_backmsg);

intent = this.getIntent();
String str = intent.getStringExtra("send");
tv_receive.setText("发送数据为:"+str);
}

public void clickBack(View v){
intent.putExtra("back",back.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
}

2.Intent实现一个电话拨号程序

androd备考 平时实验_xml_11

需要自带模拟器调试或真机调试

 

添加打电话权限:

androd备考 平时实验_xml_12

activity_main.xml 5_2

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/tv_hm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="号码:"
android:textSize="30sp" />

<EditText
android:id="@+id/et_phone"
android:layout_toRightOf="@+id/tv_hm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
/>

<Button
android:id="@+id/button1"
android:onClick="clickCall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_hm"
android:layout_below="@+id/et_phone"
android:layout_marginTop="25dp"
android:text="拨打" />

</RelativeLayout>

MainActivity.java 5_2

package com.example.glkexp05_2;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
private EditText et_phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et_phone = (EditText) findViewById(R.id.et_phone);
}

public void clickCall(View v){
String num = et_phone.getText().toString();
if(TextUtils.isEmpty(num)){
Toast.makeText(this, "号码不能为空!", 0).show();
return;
}
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+num));
startActivity(intent);
}

}

实验七 文件操作(listview)

注意获取自定义对话框内容一定view.findViewById(R.id.et_no)

listview学生记录

androd备考 平时实验_xml_13

activity_main.xml 7

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
android:id="@+id/btn_add"
android:onClick="clickAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="添加" />

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_add"
android:layout_centerHorizontal="true" >
</ListView>

</RelativeLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="@+id/image" >

<TextView
android:id="@+id/tv_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="学号:" />
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="姓名:" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="电话:" />
</LinearLayout>

</RelativeLayout>

add.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/et_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入学号" />

<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入姓名" />

<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入电话" />

</LinearLayout>

MainActivity.java 7

package com.example.glkexp07;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

List<Map<String, Object>> list;
private ListView lv;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("文件Demo");

lv = (ListView) findViewById(R.id.listView1);
list=StudentInfoUtils.read(this);
showStudent();
}

public void clickAdd(View v){
view = View.inflate(this, R.layout.add, null);
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("添加学生").setView(view);
builder.setPositiveButton("保存信息",this);
builder.setNegativeButton("关闭", null);
builder.create().show();
}

//点击保存信息按钮时的事件
@Override
public void onClick(DialogInterface dialog, int which) {
String no = ((EditText) view.findViewById(R.id.et_no)).getText().toString();
String name = ((EditText) view.findViewById(R.id.et_name)).getText().toString();
String phone = ((EditText) view.findViewById(R.id.et_phone)).getText().toString();
if(TextUtils.isEmpty(no)){Toast.makeText(this, "学号不能为空", 0).show(); return;}
if(TextUtils.isEmpty(name)){Toast.makeText(this, "姓名不能为空", 0).show();return;}
if(TextUtils.isEmpty(phone)){Toast.makeText(this, "电话不能为空", 0).show();return;}

//写数据到文件
StudentInfoUtils.save(this, no, name, phone);
//再次从文件读出数据
list=StudentInfoUtils.read(this);
showStudent();
}

//list数据显示在listview上
public void showStudent(){
//千万注意 一定在view.findViewById 否则获取不到
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item,
new String[]{"no","name","phone"},
new int[]{R.id.tv_no,R.id.tv_name,R.id.tv_phone});
lv.setAdapter(adapter);
}

}

StudentInfoUtils.java 

package com.example.glkexp07;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;

public class StudentInfoUtils {

public static String FILE_NAME="stuInfo";

public static boolean save(Context context,String no,String name,String phone){
String path = context.getFilesDir().getPath();
String info=no+"|"+name+"|"+phone+"\n";//|需要转义 用#最好 就怕 phone含了#
try {
FileOutputStream fos = context.openFileOutput(FILE_NAME, context.MODE_APPEND);
fos.write(info.getBytes());
fos.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}


public static List<Map<String, Object>> read(Context context){
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();

try {
FileInputStream fis = context.openFileInput(FILE_NAME);
BufferedReader bfr = new BufferedReader(new InputStreamReader(fis));
String line;
while((line=bfr.readLine())!=null){
Map map=new HashMap<String, Object>();
String[] split = line.split("\\|");//注意'|'要转义'\|'而'\'也要转义 就成了 '\\|'
map.put("no", "学号:"+split[0]);
map.put("name", "姓名:"+split[1]);
map.put("phone", "电话:"+split[2]);
list.add(map);
}
return list;

} catch (IOException e) {
e.printStackTrace();
return null;
}

}


}

 

模拟试题

注册用户,写入文件,到另一个activity上再读出来

 

androd备考 平时实验_java_14

androd备考 平时实验_android_15

androd备考 平时实验_android_16

activity_main.xml 模拟

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注 册 界 面"
android:textSize="30sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal" >

<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="30sp" />

<EditText
android:id="@+id/et_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="100dp" />

<TextView
android:id="@+id/tv_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="30sp" />

<EditText
android:id="@+id/et_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="100dp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >

<TextView
android:id="@+id/tv_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号:"
android:textSize="30sp" />

<EditText
android:id="@+id/et_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="100dp" />

<TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄:"
android:textSize="30sp" />

<EditText
android:id="@+id/et_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="100dp" />
</LinearLayout>

<Button
android:id="@+id/btn_regist"
android:onClick="clickRegist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:layout_marginTop="10dp"
android:textSize="20sp" />

</LinearLayout>

activity_result.xml 模拟

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20sp"
/>

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20sp"
/>

</LinearLayout>

AndroidManifest.xml

别忘了加一行:

androd备考 平时实验_java_17

MainActivity.java 模拟

package com.example.glkexp_mn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

private EditText et_name;
private EditText et_sex;
private EditText et_no;
private EditText et_age;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("注册界面");

et_name = (EditText) findViewById(R.id.et_name);
et_sex = (EditText) findViewById(R.id.et_sex);
et_no = (EditText) findViewById(R.id.et_no);
et_age = (EditText) findViewById(R.id.et_age);
}

public void clickRegist(View v) {
String name = et_name.getText().toString();
String sex = et_sex.getText().toString();
String no = et_no.getText().toString();
String age = et_age.getText().toString();
//if()可以判断下为空不允许提交
StudentInfoUtils.FILE_NAME=no+".txt";
if(StudentInfoUtils.save(this, name, sex, no, age)){
Toast.makeText(this,"保存成功!", 0).show();
}else{
Toast.makeText(this,"保存失败!", 0).show();
}
Intent intent=new Intent();
intent.setClass(this, ResultActivity.class);
startActivity(intent);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}



}

ResultActivity.java 模拟

package com.example.glkexp_mn;

import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class ResultActivity extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
setTitle("Success");

TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);

Map<String, Object> map = StudentInfoUtils.read(this);
tv1.setText("姓名:"+map.get("name")+"\t性别:"+map.get("sex"));
tv2.setText("学号:"+map.get("no")+"\t年龄:"+map.get("age"));

}
}

StudentInfoUtils.java 模拟

package com.example.glkexp_mn;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;

public class StudentInfoUtils {

public static String FILE_NAME="3160704339.txt";

public static boolean save(Context context,String name,String sex,String no,String age){
String path = context.getFilesDir().getPath();
String info=name+"|"+sex+"|"+no+"|"+age+"\n";//|需要转义 用#最好 就怕 phone含了#
try {
FileOutputStream fos = context.openFileOutput(FILE_NAME, context.MODE_APPEND);
fos.write(info.getBytes());
fos.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}


public static Map<String, Object> read(Context context){
Map<String, Object> map=new HashMap<String, Object>();
try {
FileInputStream fis = context.openFileInput(FILE_NAME);
BufferedReader bfr = new BufferedReader(new InputStreamReader(fis));
String line=bfr.readLine();
String[] split = line.split("\\|");//注意'|'要转义'\|'而'\'也要转义 就成了 '\\|'
map.put("name", split[0]);
map.put("sex", split[1]);
map.put("no", split[2]);
map.put("age", split[3]);
return map;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}


}