《Android实验报告.pdf》由会员分享,可在线阅读,更多相关《Android实验报告.pdf(8页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、1/8 创建一个名为“登录页面”的布局文件,该文件用于展示登录信息。程序界面如以下图所示:package com.example.mylayout1;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widge
2、t.EditText;import android.widget.Toast;public class Activity01 extends Activity EditText name;EditText pass;protected void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.myline);name=(EditText)findViewById(R.id.editText1);pass=(EditText)findViewById(R.i
3、d.editText2);public void click(View view)Intent intent1=new Intent(this,Mylayout1MainActivity.class);startActivity(intent1);public void click1(View view)SharedPreferences sf1=getSharedPreferences(account,Context.MODE_PRIVATE);String name1=sf1.getString(name,);String pass1=sf1.getString(pass,);String
4、 name2=name.getText().toString();String pass2=pass.getText().toString();if(name1.equals(name2)&pass1.equals(pass2)Intent intent2=new Intent(this,Textview1.class);intent2.putExtra(name,name1);intent2.putExtra(pass,pass1);startActivity(intent2);else String co=您的用户名或密码错误;Toast.makeText(this,co.toString
5、(),1000).show();private SharedPreferences getSharedPreference()return null;2/8 创建一个名为“注册页面”的布局文件,该文件用于展示登录信息。程序界面如以下图所示:package com.example.mylayout1;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;3/8 import android.content
6、.SharedPreferences.Editor;import android.view.Menu;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Spinner;import android.widget.TextView;public class Mylayout1MainActivity
7、 extends Activity EditText uname;EditText upass;RadioButton radio1;RadioButton radio2;RadioGroup group1;String sex;String name;String pass;CheckBox box1;CheckBox box2;CheckBox box3;CheckBox box4;String str1;String str2;String str3;String str4;Spinner sp;String sp1;Override protected void onCreate(Bu
8、ndle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.mylayout1_main);group1=(RadioGroup)findViewById(R.id.radioGroup1);uname=(EditText)findViewById(R.id.editText2);upass=(EditText)findViewById(R.id.editText3);box1=(CheckBox)findViewById(R.id.checkBox1);box2=(CheckBox)fi
9、ndViewById(R.id.checkBox2);box3=(CheckBox)findViewById(R.id.checkBox3);box4=(CheckBox)findViewById(R.id.checkBox4);sp=(Spinner)findViewById(R.id.spinner1);Override public boolean onCreateOptionsMenu(Menu menu)/Inflate the menu;this adds items to the action bar if it is present.getMenuInflater().infl
10、ate(R.menu.mylayout1_main,menu);4/8 return true;public void click(View view)for(int i=0;igroup1.getChildCount();i+)radio1=(RadioButton)group1.getChildAt(i);if(radio1.isChecked()sex=radio1.getText().toString();break;name=uname.getText().toString();pass=upass.getText().toString();if(box1.isChecked()st
11、r1=box1.getText().toString();else str1=;if(box2.isChecked()str2=box2.getText().toString();else str2=;if(box3.isChecked()str3=box3.getText().toString();else str3=;if(box4.isChecked()str4=box4.getText().toString();else str4=;sp1=(String)sp.getSelectedItem();Intent intent1=new Intent(this,Textview2.cla
12、ss);intent1.putExtra(name,name);intent1.putExtra(pass,pass);intent1.putExtra(sex,sex);intent1.putExtra(str,str1+str2+str3+str4);intent1.putExtra(sp1,sp1);startActivity(intent1);SharedPreferences sf1=getSharedPreferences(account,MODE_PRIVATE);Editor editor=sf1.edit();editor.putString(name,name);edito
13、r.putString(pass,pass);editormit();public void click1(View view)Intent intent2=new Intent(this,Activity01.class);startActivity(intent2);5/8 创建“登录成功页面”的界面交互代码,该程序实现用户信息显示。关键代码如下:package com.example.mylayout1;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import and
14、roid.view.Menu;import android.widget.TextView;public class Textview1 extends Activity TextView result;protected void onCreate(Bundle savedInstanceState)6/8 super.onCreate(savedInstanceState);setContentView(R.layout.activity_textview1);Intent intent1=getIntent();String str1=intent1.getStringExtra(nam
15、e);String str2=intent1.getStringExtra(pass);result=(TextView)this.findViewById(R.id.text1);result.setText(您已登录成功+n+用户名:+str1+n+密码:+str2);public boolean onCreateOptionsMenu(Menu menu)/Inflate the menu;this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.textview1,menu);
16、return true;创建“用户信息显示界面”的界面交互代码,该程序实现用户信息显示。关键代码如下:package com.example.mylayout1;import android.os.Bundle;7/8 import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.widget.TextView;public class Textview2 extends Activity TextView vi
17、ew1;TextView view2;TextView view3;TextView view4;TextView view5;Override protected void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.activity_textview2);view1=(TextView)findViewById(R.id.textView1);view2=(TextView)findViewById(R.id.textView2);view3=(T
18、extView)findViewById(R.id.textView3);view4=(TextView)findViewById(R.id.textView4);view5=(TextView)findViewById(R.id.textView5);Intent intent1=getIntent();String name=intent1.getStringExtra(name);String pass=intent1.getStringExtra(pass);String sex=intent1.getStringExtra(sex);String str=intent1.getStr
19、ingExtra(str);String sp1=intent1.getStringExtra(sp1);view1.setText(name);view2.setText(pass);view3.setText(sex);view4.setText(str);view5.setText(sp1);public void click1(View view)Intent intent1=new Intent(this,Activity01.class);startActivity(intent1);Override public boolean onCreateOptionsMenu(Menu menu)/Inflate the menu;this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.textview2,menu);return true;8/8
限制150内