1. Interface settings
1. Create a project and select EmptyActivity
2. Create a new blank Activity, named Login_Activity, and set it as the startup interface
a. Method 1: When creating, directly choose to set it as the startup interface (check LauncherActivity)
b. Method 2: Manually configure the AndroidManifest.xml file and register the activity as the startup interface
<activity android:name=".Login_Activity" android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
3. Create a corresponding layout file for this activity, and create a new activity_login.xml file in the layout folder
4. Set the overall layout to Votical
android:orientation="vertical"
5. Add a new linear layout in the layout file, and subsequent controls will be placed in it
<LinearLayout android:
layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout
6. Add a background image (home.png) to the layout: first put the image into the drawable folder, set the upper margin of the image to 100dp, and center it horizontally
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/home"
android:layout_gravity="center"
android:layout_marginTop="100dp" />
7. Add two text input boxes for entering account and password
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text"
android:layout_marginTop="20dp"
/>
<EditText
android:id="@+id/userpsd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
/>
8. Add two check buttons: remember password and automatic login, among which remember password is checked by default (this control is arranged horizontally, so create a new linear layout and set it to be arranged horizontally)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:checked="true"
android:text="记住密码"
/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="自动登录"
/>
</LinearLayout>
9. Add two buttons: one for login and one for registration
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/login"
android:text="登录"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/login"
android:text="注册"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
10. Create another blank Activity called RegisterActivity
11. Add text controls, password controls, confirm password controls and a registration button to the activity
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<EditText
android:id="@+id/username_reg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="请输入用户名"
android:inputType="textPersonName"
/>
<EditText
android:id="@+id/password_reg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="请输入密码"
android:inputType="textPassword"
/>
<EditText
android:id="@+id/password_confirm_reg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="请输入密码"
android:inputType="textPassword"
/>
<Button
android:id="@+id/regiter_reg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:text="注册"
/>
</LinearLayout>
2. Button function setting
1. Set the login button to jump to the page MainActivity and display HelloWorld
The jump between different pages uses the Intent object, which can be used to pass messages between different components
1. First create an Intent object in the current Activity, pointing to the target Activity, the first parameter is the context of the current Activity, and the second parameter is the class of the Activity
Intent intent = new Intent(this, TargetActivity.class);
2. Then start the intend
startActivity(intent);
public class LoginActivity extends AppCompatActivity {
private Button loginBtn,registerBtn;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
context=LoginActivity.this;
loginBtn=findViewById(R.id.login_btn);
registerBtn=findViewById(R.id.login_register);
loginBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(context,MainActivity.class);
startActivity(intent);
}
});
registerBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent();
intent.setClass(context,RegisterActivity.class);
startActivity(intent);
}
});
}
}
2. Button jump processing In addition to the above methods, you can also use the onclick event. At this time, you need to add the event to the button and declare the event in the activity.
<Button
android:id="@+id/login"
android:text="登录"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="login"
/>
<Button
android:id="@+id/register"
android:text="注册"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="register"
/>
public void login(View view) {
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
public void register(View view) {
Intent intent = new Intent(this,RegisterActivity.class);
startActivity(intent);
}