Android:登录界面
前端界面(布局文件) :相对布局+组件:ImageView,EditText,TextView
java代码:Toast显示消息,edtUserName.getText().toString()—>转变成字符串,setOnClickListener(按钮的监听事件)
消息提示:Toast.makeText(MainActivity.this,“用户名不正确”,Toast.LENGTH_LONG).show();
布局文件
<?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">
<RelativeLayout
android:id="@+id/rtLytTop"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="30px"
>
<ImageView
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@mipmap/qq7"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rtLytMiddle"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/rtLytTop"
android:layout_alignParentStart="true">
<EditText
android:id="@+id/edtUserName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:hint="请输入QQ号"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:scaleType="center"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
<LinearLayout
android:id="@+id/lyMiddle"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_below="@+id/rtLytMiddle"
android:layout_alignParentStart="true">
<EditText
android:id="@+id/edtPassWd"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:hint="请输入密码" />
</LinearLayout>
<Button
android:id="@+id/btnlogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:layout_below="@+id/lyMiddle"/>
<RelativeLayout
android:id="@+id/rtLytEnd"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/txt1"
android:paddingLeft="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="无法登录?"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/txt2"
android:paddingLeft="340dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="新用户"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</RelativeLayout>
Java代码
package com.example.qq.myloginapplication;
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.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText edtUserName,edtPassWd;
Button btnlogin;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.relative_layout);
//初始化组件
edtUserName = (EditText) findViewById(R.id.edtUserName);
edtPassWd = (EditText) findViewById(R.id.edtPassWd);
btnlogin = (Button) findViewById(R.id.btnlogin);
//登录按钮的监听事件
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName = edtUserName.getText().toString();
String passWd = edtPassWd.getText().toString();
if (!"qq".equals(userName)) {
Toast.makeText(MainActivity.this,"用户名不正确", Toast.LENGTH_LONG).show();
}
if (!"123".equals(passWd)) {
Toast.makeText(MainActivity.this, "密码不正确", Toast.LENGTH_LONG).show();
}
if ("qq".equals(userName) && "123".equals(passWd)) {
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();
}
}
});
}
}