Android点击按钮跳转界面

思路就是创建两个布局文件,创建两个activity文件,布局文件和activity要一一对应
第一个activity
在这里插入图片描述

package com.example.app5;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import androidx.appcompat.app.AppCompatActivity;


public class Login_Activity extends BaseActivity {

    ImageButton imageButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        imageButton=findViewById(R.id.button_login);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(Login_Activity.this,Register_Activity.class);
                startActivity(intent);//重要的是在这里,设置按键监听,设置跳转的界面
            }
        });
    }
}

第一个activity对应的布局文件
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/background_login">

    <ImageButton
        android:id="@+id/button_login"
        android:layout_width="114dp"
        android:layout_height="114dp"
        android:layout_marginTop="549dp"
        android:layout_marginLeft="170dp"
        android:src="@mipmap/login"/>
    <ImageButton
        android:id="@+id/button_login2"
        android:layout_width="114dp"
        android:layout_height="114dp"
        android:layout_marginTop="549dp"
        android:layout_marginLeft="170dp"
        android:src="@mipmap/login"/>

</LinearLayout>

第二个activity随意吧,里面不放东西也可以的
在这里插入图片描述

package com.example.app5;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.app5.data.Login;
import com.google.gson.Gson;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Register_Activity extends BaseActivity {
    Button button_Register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_password);
        button_Register = findViewById(R.id.register_button);
        button_Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
                Intent intent=new Intent();
                intent.setClass(Register_Activity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }}

第二个activity对应的布局文件
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:hint="请输入账号"
        android:id="@+id/edittext1"
        android:textSize="16sp"
        android:background="@drawable/yuanxing"
        android:layout_marginTop="48dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:hint="请输入密码"
        android:id="@+id/edittext2"
        android:textSize="16sp"
        android:layout_marginTop="10dp"
        android:background="@drawable/yuanxing"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:text="登陆"
        android:id="@+id/register_button"
        android:textSize="16sp"
        android:layout_marginTop="20dp"
        android:background="@drawable/test"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"/>

</LinearLayout>

最后记得在这里声明一下
在这里插入图片描述
下面这里是设置启动页的,就是app启动时界面
在这里插入图片描述

  • 3
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,我们可以通过监听按钮的点事件来实现页面之间的。具体实现步骤如下: 1. 在当前页面的布局文件中添加一个按钮,并设置其id和点事件监听器。 2. 在监听器中创建一个Intent对象,并设置要的目标页面。 3. 调用startActivity方法启动目标页面。 例如,我们可以在MainActivity中添加一个按钮,并在其点事件监听器中到NextActivity页面,代码如下: ``` //获取按钮 Button button = findViewById(R.id.button); //按钮进行监听 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //监听按钮,如果点,就 Intent intent = new Intent(); //前一个(MainActivity.this)是目前页面,后面一个是要的下一个页面 intent.setClass(MainActivity.this,NextActivity.class); startActivity(intent); } }); ``` 其中,R.id.button是在布局文件中设置的按钮id,NextActivity是要的目标页面。 在LoginActivity中同样可以通过类似的方式实现页面,代码如下: ``` Button button; button = findViewById(R.id.loginBtn); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 显示 //第二个页面MainActivity Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); } }); ``` 其中,R.id.loginBtn是在布局文件中设置的按钮id,MainActivity是要的目标页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值