当前位置:首页 » 《随便一记》 » 正文

Android studio 按钮点击页面跳转

2 人参与  2023年03月31日 09:29  分类 : 《随便一记》  评论

点击全文阅读


(1)先创建一个要跳转的页面,即一个新的页面,该页面是点击之后跳转的。

步骤:app--->src-->main-->res-->layout(右击)-->New-->Activity-->Empty Activity

 创建好以后,此时会生成一个同名的java文件。

初始时的界面代码如下,界面展示在后面,仅供参考。

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"    android:orientation="vertical"    android:gravity="center_horizontal"    tools:context=".MainActivity">    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="用户名:"/>        <EditText            android:id="@+id/edit1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:hint="____________"/>    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="密码:"/>        <EditText            android:id="@+id/edit2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:hint="____________"            android:password="true"/>    </LinearLayout>    <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="登录"/></LinearLayout>

跳转的界面代码,同样界面展示在后面。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:background="@drawable/bg">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="40dp"        android:textColor="#DDE0E4"        android:text="欢迎进入智慧农业系统"        /></LinearLayout>

由于页面跳转没有变换,变化的是主界面,在主界面点击按钮,使页面发生变化,因此只需要编写

MainActivity.java文件。

注:不同的文件名,包名不同,以下为我个人的编写代码,仅供参考。

package com.example.signin;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;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 Button btn;    private EditText edit1;    private EditText edit2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        btn = (Button) findViewById(R.id.btn);        edit1 = (EditText) findViewById(R.id.edit1);        edit2 = (EditText) findViewById(R.id.edit2);        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if(edit1.getText().toString().equals("aaa") &&                   edit2.getText().toString().equals("aaa")){                    Intent intent = new Intent(MainActivity.this,nextActive.class);                    startActivity(intent);                }                else {                    Toast.makeText(MainActivity.this,"输入有误,请重新输                      入",Toast.LENGTH_LONG).show();                }            }        });    }}

以下是我完成的登录跳转页面,仅供参考。

初始时的界面:

输入正确的用户名和密码以后,跳转到如下界面,仅供参考。

 如果没有输入正确,将有文字提示:


点击全文阅读


本文链接:http://zhangshiyu.com/post/57500.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1