一、题目:界面设计。
(1)根据下图开发一个单页面的APP。
(2)“清空”按钮事件:点击清空后,所输入的文字都清空。
(3)“登陆”按钮事件:点击登陆后,对比账号和密码:如果账号和密码分别对应admin和123,则提示登陆成功;否则提示登陆失败。
二、界面显示
三、代码
布局文件里的代码我用的是我们学校里教材的网格布局(老拿来主义了),也可以用其他布局。
布局文件下代码:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
android:columnCount="4" >
<TextView
android:layout_column="0"
android:layout_row="3"
android:layout_columnSpan="4"
android:layout_gravity="center_horizontal"
android:text="请输入你的账号admin与密码123"
android:textSize="20dip" />
<TextView
android:text="用户名:"
android:layout_gravity="right" />
<EditText
android:id="@+id/edit01"
android:ems="8"
android:layout_columnSpan="2"/>
<TextView
android:text="密码:"
android:layout_column="0"
android:layout_gravity="right"/>
<EditText
android:id="@+id/edit02"
android:ems="8"
android:layout_columnSpan="2" />
<Button
android:id="@+id/button01"
android:text="清空"
android:layout_column="1"
android:layout_gravity="fill_horizontal" />
<Button
android:id="@+id/button02"
android:text="登录"
android:layout_column="2"
android:layout_gravity="fill_horizontal" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_row="0"
android:layout_gravity="center_horizontal"
android:layout_column="0"
android:layout_rowSpan="3"
android:layout_columnSpan="4"
app:srcCompat="@drawable/white_nights" />
</GridLayout>
程序运行的代码用到了:
- 调用findViewById()函数分别定位xml文件中的button(清除、登录)和输入的账号密码
- 设置button的监听器
- 调用AlertDialog()函数,设置弹窗
- ifelse语句判断自己输入的字符串是否符合我设定的“admin”和“123”(只是个作业,一个下午搞出来的,不想要自行车了)
程序运行文件下代码:
package com.mwt.mwtclass1;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SignIn extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
Button button1 = (Button) findViewById(R.id.button01);
Button button2 = (Button) findViewById(R.id.button02);
EditText test01 = (EditText) findViewById(R.id.edit01);
EditText test02 = (EditText) findViewById(R.id.edit02);
Button.OnClickListener ButtonListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button01:
test01.setText("");
test02.setText("");
new AlertDialog.Builder(SignIn.this)
.setMessage("清空成功")
.show();
return;
case R.id.button02:
if (test01.getText().toString().equals("admin") && test02.getText().toString().equals("123"))
new AlertDialog.Builder(SignIn.this)
.setMessage("登录成功")
.show();
else
new AlertDialog.Builder(SignIn.this)
.setMessage("登录失败")
.show();
return;
}
}
};
button1.setOnClickListener(ButtonListener);
button2.setOnClickListener(ButtonListener);
}
}
四、演示
五、小结
希望下次还能在一个下午做完一个上机作业… …
这次的上机难度说实话只有两星⭐⭐,大概难就难在不知道怎么去清空内容,网上找的都是很难的函数,最后老师教了一个很简单粗暴的方式,就是把跟踪到的内容赋值为0字符串内容… …老懒鬼了,反正老师给的方法也很好用的样子,那就这样吧,学了半个月能搞出这种简单页面算不错了(懒鬼)。
图是网上找的,如果有问题请私聊,侵删。