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"/>