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"/>
Android Studio同时监听多个Button实现activity跳转的更多相关文章
- Android零基础入门第34节:Android中基于监听的事件处理
原文:Android零基础入门第34节:Android中基于监听的事件处理 上一期我们学习了Android中的事件处理,也详细学习了Android中基于监听的事件处理,同时学会了匿名内部类形式,那么本 ...
- android的电话监听
android的电话监听 新建一个项目,结构图如下: PhoneService: package com.demo.tingdianhua; import android.app.Service; i ...
- Android 手势水平监听判断
package com.zihao.ui; import com.zihao.R; import android.os.Bundle; import android.app.Activity; imp ...
- Android中如何监听GPS开启和关闭
转自 chenming 原文 Android中如何监听GPS开启和关闭 摘要: 本文简单总结了如何监听GPS开关的小技巧 有时需要监听GPS的开关(这种需求并不多见).实现的思路是监听代表 GPS ...
- Android手机上监听短信的两种方式
Android手机上监听短信有两种方式: 1. 接受系统的短信广播,操作短信内容. 优点:操作方便,适合简单的短信应用. 缺点:来信会在状态栏显示通知信息. AndroidManifest.xml: ...
- Android实现网络监听
一.Android Wifi常用广播 网络开发中主体会使用到的action: ConnectivityManager.CONNECTIVITY_ACTION WifiManager.WIFI_STAT ...
- android studio 2.2.2下fragment的创建和跳转
一,首先,Fragment是android应用中十分重要的一个功能,十分轻量化,也类似于activity一样,是一个个布局,可以相互跳转和传递参数.但是,它运行起来十分流畅,而且易于管理,下面是在学习 ...
- Android开发-之监听button点击事件
一.实现button点击事件的方法 实现button点击事件的监听方法有很多种,这里总结了常用的四种方法: 1.匿名内部类 2.外部类(独立类) 3.实现OnClickListener接口 4.添加X ...
- Fragment中Button的android:onClick 无法监听相应
在Fragment的布局文件中,Button控件下添加android:onClick监听: 1.fragment_main.xml <RelativeLayout xmlns:android=& ...
随机推荐
- 痞子衡嵌入式:其实i.MXRT1050,1020,1015系列ROM也提供了FlexSPI driver API
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT1050/1020/1015系列ROM中的FlexSPI驱动API使用. 今天痞子衡去4S店给爱车做保养了,保养一次要等两小 ...
- 解决nginx 出现 413:Request Entity Too Large
去网上搜了一下,说是上传文件大小超过nginx的限制大小(nginx据说默认只能上传不超过2MB的文件) 解决方法: #nginx/conf/nginx.conf http { ... client_ ...
- redis(二十三):Redis 集群(proxy 型)二
redis的确是一个非常高效的缓存服务器,但是单台redis服务器的内存管理能力有限,如果一味的加大内存的话会导致redis服务器的性能下降,所以就必须要搭建redis集群来提供服务.在redis官方 ...
- Fisher算法+两类问题
文章目录 一.Fisher算法 二.蠓的分类问题: 三.代码实现: 一.Fisher算法 二.蠓的分类问题: 两种蠓Af和Apf已由生物学家根据它们的触角和翼长加以区分(Af是能传播花粉的益虫,Apf ...
- 10种常见OOM分析——手把手教你写bug
点赞+收藏 就学会系列,文章收录在 GitHub JavaKeeper ,N线互联网开发必备技能兵器谱,笔记自取 在<Java虚拟机规范>的规定里,除了程序计数器外,虚拟机内存的其他几个运 ...
- Linux指令,更新中
Linux指令 ls 列举文件. ls *.txt:列举所有txt后缀的文件夹. touch 创建文件. $ touch asd.txt fgh.txt $ touch love{1..10}shiy ...
- 学完自动化测试,用小技能做了点兼职刷弹幕,小赚10W
大家好,今天又给大家带来了Python爬虫的分享,继续来研究一下虎牙平台的爬虫. 起因 我冒出有一个很有趣的想法,就是,我们可以使用selenium来完成虎牙自动化登录,并且自动给主播发送弹幕功能的程 ...
- ubuntu安装 jdk
首先查一下当前JDK的版本 ,一般是系统自带Jdk$ java -version 然后根据版本8安装完整版的OpenJDK $ sudo apt-get install openjdk-8-jdk 如 ...
- React Native 中使用Redux
参考https://jspang.com/detailed?id=48和印度同事的代码简单整理一下在RN中使用Redux的步骤 1. 首先我们应该先了解Redux是什么,什么情况下需要用到它 在Red ...
- for循环实现Fibonacci数列
Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n. 输出格 ...