学习Android 开发第一天,做了这么一个小界面。
登录界面
布局资源
头像
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:gravity="center">
<ImageView
android:layout_width="400px"
android:layout_height="400px"
android:src="@mipmap/user_img" />
</LinearLayout>
输入框
用户名
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="100px"
android:layout_height="100px"
android:src="@mipmap/xiao_user" />
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/user_name"
android:singleLine="true" />
</LinearLayout>
密码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="100px"
android:layout_height="100px"
android:src="@mipmap/yao_shi" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/user_password"
android:inputType="textPassword"
android:singleLine="true" />
</LinearLayout>
按钮
登录
<Button
android:id="@+id/login"
android:layout_width="250px"
android:layout_height="150px"
android:layout_marginRight="50px"
android:background="@drawable/button"
android:text="@string/login"
android:textSize="60px" />
退出
<Button
android:id="@+id/back"
android:layout_width="250px"
android:layout_height="150px"
android:background="@drawable/button"
android:text="@string/back"
android:textSize="60px" />
主布局界面
import androidx.appcompat.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 {
private EditText username;
private EditText password;
private Button login;
private Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局资源设置用户界面
setContentView(R.layout.activity_main);
//获取控件实例
username = findViewById(R.id.username);
password = findViewById(R.id.password);
login = findViewById(R.id.login);
back = findViewById(R.id.back);
//为登录按钮设置监听,实现监听器接口,编写事件处理方法
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取用户输入的信息
String input_username = username.getText().toString().trim();
String input_password = password.getText().toString().trim();
// 判断用户是否登录成功
if (input_username.equals("wen") && input_password.equals("123")) {
Toast.makeText(MainActivity.this, "登录成功了", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_LONG).show();
// 清空用户名和密码
username.setText("");
password.setText("");
// 用户名获取焦点
username.requestFocus();
}
}
});
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 关闭当前窗口
finish();
}
});
}
}