• Android Studio同时监听多个Button实现activity跳转


       MainActivity.java:

    package com.example.test;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    
    public class MainActivity extends AppCompatActivity {
    
         Button mBtnTextView1;
         Button mBtnTextView2;
        Intent intent1,intent2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mBtnTextView1 = findViewById(R.id.btnTextView1);
            mBtnTextView1.setOnClickListener(new ButtonListener());
            mBtnTextView2 = findViewById(R.id.btnTextView2);
            mBtnTextView2.setOnClickListener(new ButtonListener());
        };
    
        private class ButtonListener implements View.OnClickListener{
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.btnTextView1:
                        intent1 = new Intent(MainActivity.this,TestTextViewActivity1.class);
                        startActivity(intent1);
                        break;
                    case R.id.btnTextView2:
                        intent2 = new Intent(MainActivity.this,TestTextViewActivity2.class);
                        startActivity(intent2);
                        break;
                }
    
            }
        }
    }

      

      activity_main.xml

    <?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"
        >
    
        <Button
            android:id="@+id/btnTextView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TestTextView1" />
    
        <Button
            android:id="@+id/btnTextView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TestTextView2"/>
    
    </LinearLayout>

      TestTextViewActivity1.java:

    package com.example.test;
    
    import android.graphics.Paint;
    import android.os.Bundle;
    import android.widget.TextView;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    public class TestTextViewActivity1 extends AppCompatActivity {
    
        private TextView mtv1;
        private TextView mtv2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_text_view1);
            mtv1 = findViewById(R.id.ttv1);
        }
    }

      activity_text_view1.xml

    <?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"
        >
    
        <TextView
            android:id="@+id/ttv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="This is a test"
            android:textColor="#5500FF"
            android:textSize="32sp"
            android:padding="10dp"
            />
    
    </LinearLayout>

      

      TestTextViewActivity2.java:

    package com.example.test;
    
    import android.graphics.Paint;
    import android.os.Bundle;
    import android.widget.TextView;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    public class TestTextViewActivity2 extends AppCompatActivity {
    
        private TextView mtv2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_text_view2);
            mtv2 = findViewById(R.id.ttv2);
        }
    }

      activity_text_view2.xml

    <?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"
        >
    
        <TextView
            android:id="@+id/ttv2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello everybody"
            android:textColor="#E91E63"
            android:textSize="32sp"
            android:padding="10dp"
            />
    
    </LinearLayout>

    注意在AndroidMainifest.xml中添加

    <activity android:name=".TestTextViewActivity1"/>
    <activity android:name=".TestTextViewActivity2"/>

       

  • 相关阅读:
    【求教】
    【刷题】【dp】【贪心】D. Nastya and Scoreboard
    【笔记】STL的七种武器(三)算法
    【刷题】【stl】【字符串】反片语
    【刷题】【字符串】加入数字
    【笔记】【字符串】c++的输入总结
    【刷题】【dfs】【暴力】不如来上分
    【笔记】c语言学习记录
    【笔记】实践中学C语言 100例
    【笔记】STL的七种武器(二)关联容器
  • 原文地址:https://www.cnblogs.com/xhj1074376195/p/12295991.html
Copyright © 2020-2023  润新知