您的当前位置:首页正文

Android实验报告

2023-01-19 来源:好走旅游网
创建一个名为“登录页面”的布局文件,该文件用于展示登录信息。程序界面如下图所示:

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.widget.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.id.editText2);}

public void click(View view){

Intent intent1=new Intent(this,Mylayout1MainActivity.class); startActivity(intent1);}

public void click1(View view){

SharedPreferences

sf1=getSharedPreferences(\"account\

String name1=sf1.getString(\"name\String pass1=sf1.getString(\"pass\String 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\intent2.putExtra(\"pass\startActivity(intent2);}

else{

String co=\"您的用户名或密码错误\";

Toast.makeText(this, co.toString(), 1000).show();}}

private SharedPreferences getSharedPreference() {

return null;}}

创建一个名为“注册页面”的布局文件,该文件用于展示登录信息。程序界面如下图所示:

package com.example.mylayout1; import android.os.Bundle; import android.app.Activity;

import android.content.Intent;

import android.content.SharedPreferences; import android.content.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 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(Bundle 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)findViewById(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().inflate(R.menu.mylayout1_main, menu); return true;}

public void click(View view){

for(int i=0;iradio1=(RadioButton)group1.getChildAt(i); if(radio1.isChecked()){

sex=radio1.getText().toString(); break;}}

name=uname.getText().toString(); pass=upass.getText().toString();

if(box1.isChecked())str1=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.class); intent1.putExtra(\"name\

intent1.putExtra(\"pass\ intent1.putExtra(\"sex\

intent1.putExtra(\"str\ intent1.putExtra(\"sp1\ startActivity(intent1);

SharedPreferences sf1=getSharedPreferences(\"account\ Editor editor=sf1.edit(); editor.putString(\"name\ editor.putString(\"pass\ editor.commit(); } public void click1(View view){

Intent intent2=new Intent(this,Activity01.class); startActivity(intent2); }}

创建“登录成功页面”的界面交互代码,该程序实现用户信息显示。关键代码如下:

package com.example.mylayout1; import android.os.Bundle; import android.app.Activity; import android.content.Intent;

import android.view.Menu; import android.widget.TextView; public class Textview1 extends Activity {

TextView result;

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_textview1); Intent intent1=getIntent();

String str1=intent1.getStringExtra(\"name\"); 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); return true;}}

创建“用户信息显示界面”的界面交互代码,该程序实现用户信息显示。关键代码如下:

package com.example.mylayout1; import android.os.Bundle; 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 view1; 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=(TextView)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.getStringExtra(\"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){ } }

@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;

Intent intent1=new Intent(this, Activity01.class); startActivity(intent1);

因篇幅问题不能全部显示,请点此查看更多更全内容