文章目录
题目
模仿QQ登录界面(或者其它应用程序的登录界面)设计一个应用。要求在第一个界面输入用户名和密码,点击“登录”按钮,若用户名和密码正确,则跳转到欢迎界面
第一版
实现的功能很简单:
- 检测一个正确的账户和密码
- 一个简单的跳转界面(这个Activity输出类似“用户名”)
所以之后有空了肯定是从这两个方面优化。
参考
用了这个登陆判断函数:Android Studio实现简单的登陆界面
用了之前简单的跳转界面:【AS入门笔记】一个简单的的响应程序
主界面java文件为MainActivity.java
在这里直接设置了一个公户名和密码作为测试。
package com.example.wangyifan.calculator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public final static String EXTRA_MESSAGE = "it.uibe.edu.cn.myapplication.MESSAGE";
EditText edt1;
EditText edt2;
Button btn;
Toast mtoast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text2);
edt1=(EditText)findViewById(R.id.edit1);
edt2=(EditText)findViewById(R.id.edit2);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v){
if (edt1.getText().toString().equals("")||edt2.getText().toString().equals("")) {
showToast("用户名或密码不能为空!");
}else if(edt1.getText().toString().equals("1805120720")&&edt2.getText().toString().equals("666666")){
showToast("登录成功!!!");
Intent intent=new Intent(this, DisplayMessageActivity.class);
EditText editText=(EditText)findViewById(R.id.edit1);
String message=editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}else{
showToast("用户名或密码错误!");
}
}
private void showToast(String msg){
mtoast=Toast.makeText(this,msg,Toast.LENGTH_SHORT);
mtoast.show();
}
}
主界面命名布局文件为text2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0,3">
<ImageView
android:id="@+id/iv"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@mipmap/ic_launcher"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<LinearLayout
android:id="@+id/ll_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/iv"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:background="#ffffff"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="20sp"
android:textColor="#000"
android:padding="10dp"/>
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:padding="10dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#ffffff"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_below="@+id/ll_number"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="20dp"
android:textColor="#000"
android:padding="10dp"/>
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:inputType="textPassword"
android:padding="10dp"/>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:text="登录"
android:background="#3C8DC4"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_below="@+id/ll_password"
android:layout_marginTop="50dp"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:textColor="#000"
android:textSize="20sp"
android:text=""
android:layout_below="@+id/bt"
android:layout_alignLeft="@+id/bt"
android:layout_alignStart="@+id/bt"/>
</RelativeLayout>
跳转界面java文件为DisplayMessageActivity.java
package com.example.wangyifan.calculator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent=getIntent();
String message =intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView=(TextView)findViewById(R.id.text_show_msg);
textView.setTextSize(40);
textView.setText(message);
}
}
跳转界面命名布局文件为activity_display_message.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.wangyifan.myapplication.DisplayMessageActivity">
<TextView android:id="@+id/text_show_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>