MainActivity:
package com.example.myapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
//public class MainActivity extends AppCompatActivity {
//
//
//
// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
//
// Button button; //声明组件
// button=findViewById(R.id.button_1); //找到组件
// button.setOnClickListener(new View.OnClickListener() { //为组件设置点击事件
// public void onClick(View v) {
// Toast.makeText(MainActivity.this,"hello",Toast.LENGTH_SHORT).show();
// }
// });
// }
//}
public class MainActivity extends AppCompatActivity {
private Button Login_btn;
private Button Setting_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Setting_btn = (Button) findViewById(R.id.setting_btn);
Login_btn = (Button) findViewById(R.id.login_btn);
Login_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent=new Intent();//Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件。
intent.setClass(MainActivity.this, MainActivity.class);
startActivity(intent);}
});
Setting_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);//跳转执行,没有这句话无法执行
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="用户名"
android:inputType="textLongMessage"
android:textColor="#000000"
android:textSize="16sp"
android:layout_marginTop="50dp"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="密码"
android:inputType="numberPassword"
android:textColor="#000000"
android:textSize="16sp"
android:layout_below="@id/username"/>
<Button
android:id="@+id/login_btn"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@id/password"
android:layout_alignParentStart="true"
android:layout_marginStart="6dp"
android:layout_marginTop="50dp"
android:text="登录"
android:textSize="16sp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp" />
<Button
android:id="@+id/setting_btn"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@id/password"
android:layout_alignParentEnd="true"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="50dp"
android:text="设置"
android:textSize="16sp"
android:layout_alignParentRight="true" />
</RelativeLayout>