移动第二次作业
九宫格
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button1" android:layout_centerInParent="true" android:layout_width="80dp" android:layout_height="80dp" android:background="#000000" android:text="" /> <Button android:id="@+id/button2" android:layout_width="80dp" android:layout_height="80dp" android:background="#00FF00" android:layout_toLeftOf="@+id/button1" android:layout_centerVertical="true" android:text="" /> <Button android:id="@+id/button3" android:layout_width="80dp" android:layout_height="80dp" android:background="#ff00ff" android:layout_above="@+id/button1" android:layout_centerHorizontal="true" android:text="" tools:layout_editor_absoluteX="80dp" tools:layout_editor_absoluteY="165dp"/> <Button android:id="@+id/button4" android:layout_width="80dp" android:layout_height="80dp" android:background="#0000ff" android:layout_above="@+id/button2" android:layout_toLeftOf="@+id/button3" android:text="" /> <Button android:id="@+id/button5" android:layout_width="80dp" android:layout_height="80dp" android:layout_toRightOf="@id/button1" android:background="#FFFFEB3B" android:layout_centerInParent="true" android:text="" /> <Button android:id="@+id/button6" android:layout_width="80dp" android:layout_height="80dp" android:layout_above="@id/button5" android:layout_toRightOf="@id/button3" android:background="#FFFF9800" android:layout_centerInParent="true" android:text="" /> <Button android:id="@+id/button7" android:layout_width="80dp" android:layout_height="80dp" android:layout_below="@id/button1" android:background="#FF03A9F4" android:layout_centerInParent="true" android:text="" /> <Button android:id="@+id/button8" android:layout_width="80dp" android:layout_height="80dp" android:layout_below="@id/button5" android:layout_toRightOf="@id/button7" android:background="#FFE91E63" android:layout_centerInParent="true" android:text="" /> <Button android:id="@+id/button9" android:layout_width="80dp" android:layout_height="80dp" android:layout_below="@id/button2" android:layout_toLeftOf="@id/button7" android:background="#FF9C27B0" android:layout_centerInParent="true" android:text="" />
</RelativeLayout>
登录界面
package com.example.homework2; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button blogin = (Button)findViewById(R.id.enter);//先引用组件,然后执行算法 blogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String user = "zhangsan"; String password = "123456"; EditText u = (EditText)findViewById(R.id.etname); EditText p = (EditText)findViewById(R.id.etpass); if (u.getText().toString().equals(user)){//判断用户名是否正确 //读取文本信息,转为字符串,判断正确 if (p.getText().toString().equals(password)){ //消息提示 Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show(); }else{ //密码输入错误 Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_LONG).show(); } }else{ //用户名错误提示 Toast.makeText(MainActivity.this, "用户名错误", Toast.LENGTH_LONG).show(); } } }); } }