《移动应用程序设计基础》实 验 报 告(二)
实验名称:
用户登录实验
所使用的工具软件及环境:
JDK1.8,Android Studio
一、实验目的:
本实验通过实现用户登录界面,使得学员能进一步加深和掌握Android的常用基本控件使用,包括:TextView,EditView、CheckBox、Button、ImageView以及ProgressBar等。
二、实验内容:
- 构建登录界面布局login.xml,放置各类按钮,如下图;
- 编辑
- 编写Activity代码LoginActivity.java,实现EditView的username、password和CheckBox的issaved控件发现取得;
- 编写进度条ProgressBar的界面布局progress.xml,放置水平进度条,设置最大值属性为100;
- 编写ProgressActivity.java代码,通过线程和Handler模拟实现ProgressBar的进度条的进度值变动,每100毫秒增加1;
- 点击Submit按钮,页面跳转到进度条界面,进度条启动,当进度值达到最大(100)时,关闭进度条,并将用户名(username)信息返回登录页面,通过Toast弹出信息框提示登录成功或失败原因。
- 提示:页面跳转可以使用startActivityForResult方法,返回结果可以覆写onActivityResult方法,设置返回值可以使用setResult方法。
- 实验结果
- 编辑编辑
三、源代码
//activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".MainActivity"><![CDATA[ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:layout_editor_absoluteX="162dp" tools:layout_editor_absoluteY="80dp"> ]]> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="72dp" android:orientation="horizontal"></LinearLayout> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="323dp" android:layout_height="153dp" android:orientation="horizontal"> <LinearLayout android:layout_width="85dp" android:layout_height="match_parent" android:orientation="vertical"></LinearLayout> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="149dp" app:srcCompat="@drawable/logo" tools:ignore="MissingConstraints" /> </LinearLayout> <FrameLayout android:layout_width="375dp" android:layout_height="75dp"> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="102dp" android:orientation="horizontal"> <FrameLayout android:layout_width="101dp" android:layout_height="match_parent"> </FrameLayout> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SYSTEM LOGIN" android:textSize="30sp" tools:ignore="MissingConstraints" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="407dp" android:layout_height="138dp" android:orientation="vertical" tools:ignore="MissingConstraints"> <LinearLayout android:layout_width="381dp" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="45dp" android:layout_height="match_parent" android:orientation="horizontal"></LinearLayout> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Username:" android:textSize="20dp" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="481dp" /> <EditText android:id="@+id/usename_edtxt" android:layout_width="244dp" android:layout_height="wrap_content" android:drawablePadding="12dp" android:gravity="center|left" android:hint="请输入用户名" android:textColorHint="@android:color/darker_gray" android:textSize="24sp" tools:ignore="MissingConstraints" /> </LinearLayout> <LinearLayout android:layout_width="382dp" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="45dp" android:layout_height="match_parent" android:orientation="horizontal"></LinearLayout> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Password:" android:textSize="20dp" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="538dp" /> <EditText android:id="@+id/password_edtxt" android:layout_width="272dp" android:layout_height="55dp" android:drawablePadding="12dp" android:gravity="center|left" android:hint="请输入密码" android:inputType="textPassword" android:textColorHint="@android:color/darker_gray" android:textSize="24sp" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="117dp" tools:layout_editor_absoluteY="523dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:layout_width="262dp" android:layout_height="match_parent" android:orientation="horizontal"> </LinearLayout> <CheckBox android:id="@+id/save_password_checkBox" android:layout_width="346dp" android:layout_height="match_parent" android:text="Save Password" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="387dp" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="183dp" android:layout_height="15dp" android:orientation="horizontal"> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:layout_width="39dp" android:layout_height="70dp" android:orientation="vertical"> </LinearLayout> <Button android:id="@+id/submit_button" android:layout_width="335dp" android:layout_height="wrap_content" android:backgroundTint="@color/black" android:text="submit" /> </LinearLayout> </LinearLayout> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
//MainActivity.java package com.example.login; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; public class MainActivity extends AppCompatActivity { static public EditText usenameEdtxt,passwordEdtxt; static public CheckBox savePasswordCheckBox; public Button submitButton; public Boolean Selectcheckbox; static public String select; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); usenameEdtxt=(EditText)findViewById(R.id.usename_edtxt); passwordEdtxt=(EditText)findViewById(R.id.password_edtxt); savePasswordCheckBox=(CheckBox)findViewById(R.id.save_password_checkBox); submitButton=(Button)findViewById(R.id.submit_button); //登录按钮监控 submitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String usename=usenameEdtxt.getText().toString(); String password=passwordEdtxt.getText().toString(); if(usename.isEmpty()){ usenameEdtxt.setError("用户名不能为空"); usenameEdtxt.requestFocus(); return; } if(password.isEmpty()){ passwordEdtxt.setError("密码不能为空"); passwordEdtxt.requestFocus(); return; } if(usename.length()>0&&password.length()>0){ Selectcheckbox=savePasswordCheckBox.isChecked(); if(Selectcheckbox)select="已勾选"; else select="未勾选"; Intent it=new Intent(); Context packageContext; it.setClass(MainActivity.this, ProgressActivity.class); MainActivity.this.startActivity(it); } } }); } }
// progress.xml <?xml version="1.0" encoding="utf-8"?> <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=".ProgressActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="76dp" android:orientation="vertical"> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="169dp" android:orientation="horizontal"> <LinearLayout android:layout_width="45dp" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> <ProgressBar android:id="@+id/horizontal_pbar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="322dp" android:layout_height="97dp" android:max="100" /> </LinearLayout> </LinearLayout>
// ProgressActivity.java package com.example.login; import android.os.Bundle; import android.os.Handler; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class ProgressActivity extends AppCompatActivity { //MainActivity mainActivity=new MainActivity(); private ProgressBar mProgress; private Handler mHandler = new Handler(); public String a="admin"; private Boolean FLAG=true; private Button startpbarbtn; //private TextView cBoxStateTxt; int mProgressStatus=0; protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.progress); mProgress = (ProgressBar) findViewById(R.id.horizontal_pbar); //设置监听按钮 //startpbarbtn = (Button)findViewById(R.id.start_pbar_btn); //设置监视内容 //监听 new Thread(new Runnable() { public void run() { //推荐使用标志位使其不执行代码,不推荐使用true while (FLAG) { mProgressStatus = addNum(); // 更新进度条 // post方法推送一个Runnable到Handler,可以在此更新UI mHandler.post(new Runnable() { public void run() { if (mProgressStatus < 100) { mProgress.setProgress(mProgressStatus); mProgressStatus++; } //执行完成后隐藏进度条并将标志设置为false else { mProgress.setVisibility(View.INVISIBLE); FLAG = false; if(MainActivity.usenameEdtxt.getText().toString().equals(a)&& MainActivity.passwordEdtxt.getText().toString().equals(a)) { Toast toast = Toast.makeText(getApplicationContext(), "登录成功Usename:"+MainActivity.usenameEdtxt.getText().toString()+ "\nPassword:"+MainActivity.passwordEdtxt.getText().toString()+ "\ncheckbox状态:"+MainActivity.select, Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } else{ Toast toast = Toast.makeText(getApplicationContext(), "登录失败Usename:"+MainActivity.usenameEdtxt.getText().toString()+ "\nPassword:"+MainActivity.passwordEdtxt.getText().toString()+ "\ncheckbox状态:"+MainActivity.select, Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } finish(); } } }); } } private int addNum() { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } return mProgressStatus; } }).start(); //cBoxStateTxt.setText("CheckBox状态: "+mainActivity.select); } }
四、实验结果测试
编辑
编辑
编辑
编辑
编辑
此时输入设置好的username和password,勾选或不勾选save password,点击submit按钮会跳转到进度条界面,进度条满后会返回界面,出现登录成功还有输入的username和password且save password的勾选状态的小提示。同时如果输入错误的username和password会出现登录错误还有其他部分的小提示。
五、收获与体会
通过此次实验,使得更能进一步加深和掌握Android的常用基本控件使用,对基本控件有了基本的认识,而且对以前的知识有了进一步的复习与巩固,对今后android的app制作有了较大的进步与学习。