这是我AndroID学习的第一天,第一堂课的作业是写两个button,分别实现点击显示hello world 和图片消息。
实现代码如下:
activity_main.xml:
1 <?xml version="1.0" enCoding="utf-8"?> 2 <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" 3 xmlns:app="http://schemas.androID.com/apk/res-auto" 4 xmlns:tools="http://schemas.androID.com/tools" 5 androID:layout_wIDth="match_parent" 6 androID:layout_height="match_parent" 7 tools:context="com.example.zhangqiongwen.homework1.MainActivity" 8 tools:layout_editor_absoluteY="81dp"> 9 10 <include layout="@layout/button">11 12 </include>13 </relativeLayout>
button.xml:
1 <?xml version="1.0" enCoding="utf-8"?> 2 <relativeLayout 3 xmlns:androID="http://schemas.androID.com/apk/res/androID" 4 androID:layout_wIDth="match_parent" 5 androID:layout_height="match_parent"> 6 7 8 <relativeLayout 9 androID:ID="@+ID/button_layout"10 androID:layout_wIDth="380dp"11 androID:layout_height="wrap_content"12 androID:layout_alignParentStart="true"13 androID:layout_alignParenttop="true">14 15 <button16 androID:ID="@+ID/button1"17 androID:layout_wIDth="120dp"18 androID:layout_height="40dp"19 androID:layout_alignParenttop="true"20 androID:layout_alignParentleft="true"21 androID:onClick="click"22 androID:text="button1" />23 24 <button25 androID:ID="@+ID/button2"26 androID:layout_wIDth="120dp"27 androID:layout_height="40dp"28 androID:onClick="click"29 androID:layout_alignParenttop="true"30 androID:layout_alignParentRight="true"31 androID:text="button2" />32 </relativeLayout>33 34 35 </relativeLayout>
page1.xml:
1 <?xml version="1.0" enCoding="utf-8"?> 2 <relativeLayout 3 xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" 4 androID:layout_height="match_parent"> 5 6 <include 7 layout="@layout/button"> 8 </include> 9 10 <TextVIEw11 androID:layout_wIDth="wrap_content"12 androID:layout_height="wrap_content"13 androID:layout_centerVertical="true"14 androID:layout_centerHorizontal="true"15 androID:text="Hello World!!"16 androID:textcolor="#FF79BC"17 />18 19 </relativeLayout>
page2.xml:
1 <?xml version="1.0" enCoding="utf-8"?> 2 <relativeLayout 3 xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" 4 androID:layout_height="match_parent"> 5 6 <include layout="@layout/button"/> 7 8 <linearLayout 9 androID:layout_wIDth="match_parent"10 androID:layout_height="300dp"11 androID:layout_margintop="40dp"12 >13 14 <ImageVIEw15 androID:ID="@+ID/picture1"16 androID:layout_wIDth="match_parent"17 androID:layout_height="match_parent"18 androID:layout_alignParentStart="true"19 androID:layout_alignParenttop="true"20 androID:src="@drawable/pictur1" />21 22 </linearLayout>23 24 </relativeLayout>
activity.java:
1 package com.example.zhangqiongwen.homework1; 2 3 import androID.app.Activity; 4 import androID.content.Intent; 5 import androID.os.Bundle; 6 import androID.support.annotation.Nullable; 7 import androID.vIEw.VIEw; 8 import androID.Widget.button; 9 10 11 public class MainActivity extends Activity{12 @OverrIDe13 protected voID onCreate(@Nullable Bundle savedInstanceState) {14 super.onCreate(savedInstanceState);15 this.setContentVIEw(R.layout.activity_main);16 17 button btn1 = (button)findVIEwByID(R.ID.button1);18 button btn2 = (button)findVIEwByID(R.ID.button2);19 20 21 btn1.setonClickListener(new VIEw.OnClickListener() {22 @OverrIDe23 public voID onClick(VIEw vIEw) {24 Intent i = new Intent(MainActivity.this , page1.class);25 startActivity(i);26 }27 });28 29 btn2.setonClickListener(new VIEw.OnClickListener() {30 @OverrIDe31 public voID onClick(VIEw vIEw) {32 Intent b = new Intent(MainActivity.this , page2.class);33 startActivity(b);34 }35 });36 37 }38 }
page1.java
1 import androID.app.Activity; 2 import androID.content.Intent; 3 import androID.os.Bundle; 4 import androID.support.annotation.Nullable; 5 import androID.vIEw.ContextMenu; 6 import androID.vIEw.VIEw; 7 import androID.Widget.button; 8 import androID.Widget.relativeLayout; 9 10 /**11 * Created by zhang.qiongwen on 2019/8/10.12 */13 14 public class page1 extends Activity {15 16 @OverrIDe17 protected voID onCreate(@Nullable Bundle savedInstanceState) {18 super.onCreate(savedInstanceState);19 20 setContentVIEw(R.layout.page1);21 22 23 button btn1 = (button)findVIEwByID(R.ID.button1);24 button btn2 = (button)findVIEwByID(R.ID.button2);25 26 btn1.setonClickListener(new VIEw.OnClickListener() {27 @OverrIDe28 public voID onClick(VIEw vIEw) {29 Intent i = new Intent(page1.this , page1.class);30 startActivity(i);31 }32 });33 34 btn2.setonClickListener(new VIEw.OnClickListener() {35 @OverrIDe36 public voID onClick(VIEw vIEw) {37 Intent b = new Intent(page1.this , page2.class);38 startActivity(b);39 }40 });41 42 43 }44 }
page2.java
1 package com.example.zhangqiongwen.homework1; 2 3 import androID.app.Activity; 4 import androID.content.Intent; 5 import androID.os.Bundle; 6 import androID.support.annotation.Nullable; 7 import androID.vIEw.VIEw; 8 import androID.Widget.button; 9 10 /**11 * Created by zhang.qiongwen on 2019/8/10.12 */13 14 public class page2 extends Activity {15 16 @OverrIDe17 protected voID onCreate(@Nullable Bundle savedInstanceState) {18 super.onCreate(savedInstanceState);19 20 setContentVIEw(R.layout.page2);21 22 button btn1 = (button)findVIEwByID(R.ID.button1);23 button btn2 = (button)findVIEwByID(R.ID.button2);24 25 btn1.setonClickListener(new VIEw.OnClickListener() {26 @OverrIDe27 public voID onClick(VIEw vIEw) {28 Intent i = new Intent(page2.this , page1.class);29 startActivity(i);30 }31 });32 33 btn2.setonClickListener(new VIEw.OnClickListener() {34 @OverrIDe35 public voID onClick(VIEw vIEw) {36 Intent b = new Intent(page2.this , page2.class);37 startActivity(b);38 }39 });40 41 42 }43 }
其中使用了intent来连接page1、page2、activity三个事件,分别展示page1.xml和page2.xml,其中page1.java和page2.java都需要来绑定button的点击事件,否则在跳转到page1和page2事件之后无法响应另一个button的点击事件,程序直接关闭,关于intent用法可以参考以下链接https://blog.csdn.net/weixin_38199770/article/details/79391259
另外也可以通过直接判断发生点击事件的button来响应点击事件,代码如下:
MainActivity.java:
1 package com.example.zhangqiongwen.homework1; 2 3 import androID.app.Activity; 4 import androID.content.Intent; 5 import androID.os.Bundle; 6 import androID.support.annotation.Nullable; 7 import androID.vIEw.VIEw; 8 import androID.vIEw.VIEwStructure; 9 import androID.Widget.button;10 11 12 public class MainActivity extends Activity{13 @OverrIDe14 protected voID onCreate(@Nullable Bundle savedInstanceState) {15 super.onCreate(savedInstanceState);16 this.setContentVIEw(R.layout.activity_main);17 18 19 20 }21 22 public voID click(VIEw vIEw){23 int i = vIEw.getID();24 25 switch (i){26 27 case R.ID.button1:28 this.setContentVIEw(R.layout.page1);29 break;30 31 case R.ID.button2:32 this.setContentVIEw(R.layout.page2);33 break;34 35 }36 }37 38 }
另外还有同事给我讲了另外一种思路,将图片和Hellow World文字放在同一个布局文件下,然后按钮点击的作用分别是隐藏TextVIEw、显示ImageVIEw和显示TextVIEw、隐藏ImageVIEw,我在网上进行查找发现似乎要使用ConstrainLayout的布局方式,我现在勉强能够使用relativelayout布局和linerlayout布局,问题留存,等待解决。
总结以上是内存溢出为你收集整理的Android响应点击事件页面跳转全部内容,希望文章能够帮你解决Android响应点击事件页面跳转所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)