【安卓开发】按钮点击事件

秒速五厘米 2023-07-21 08:34 130阅读 0赞

1783030-20200329202217042-670825903.png

布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:background="#00BCD4"
  12. android:orientation="vertical">
  13. <LinearLayout
  14. android:layout_width="match_parent"
  15. android:layout_height="215dp"
  16. android:background="#FFFFFF"
  17. android:orientation="horizontal">
  18. <ImageView
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_margin="10dp"
  22. android:src="@drawable/tu1" />
  23. </LinearLayout>
  24. <LinearLayout
  25. android:layout_width="match_parent"
  26. android:layout_height="match_parent"
  27. android:background="#FFFFFF"
  28. android:orientation="vertical">
  29. <LinearLayout
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:background="#FFFFFF"
  33. android:orientation="horizontal">
  34. <ImageView
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_weight="1"
  38. android:id="@+id/image1"
  39. android:src="@mipmap/ic_launcher"></ImageView>
  40. <ImageView
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:layout_weight="1"
  44. android:src="@mipmap/ic_launcher"></ImageView>
  45. <ImageView
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:layout_weight="1"
  49. android:src="@mipmap/ic_launcher"></ImageView>
  50. </LinearLayout>
  51. <LinearLayout
  52. android:layout_width="match_parent"
  53. android:layout_height="wrap_content"
  54. android:background="#FFFFFF"
  55. android:orientation="horizontal">
  56. <ImageView
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:layout_weight="1"
  60. android:src="@mipmap/ic_launcher"></ImageView>
  61. <ImageView
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:layout_weight="1"
  65. android:src="@mipmap/ic_launcher"></ImageView>
  66. <ImageView
  67. android:layout_width="wrap_content"
  68. android:layout_height="wrap_content"
  69. android:layout_weight="1"
  70. android:src="@mipmap/ic_launcher"></ImageView>
  71. </LinearLayout>
  72. <Button
  73. android:layout_width="wrap_content"
  74. android:layout_height="wrap_content"
  75. android:text="OPEN"
  76. android:id="@+id/Button1"
  77. >
  78. </Button>
  79. <TextView
  80. android:layout_width="wrap_content"
  81. android:layout_height="wrap_content"
  82. android:text="Hello,world!"
  83. android:id="@+id/text1"
  84. >
  85. </TextView>
  86. </LinearLayout>
  87. </LinearLayout>
  88. </androidx.constraintlayout.widget.ConstraintLayout>

Java控制文件

  1. package com.huangenet.myapplication;
  2. import android.annotation.SuppressLint;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.ImageView;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. public class MainActivity extends AppCompatActivity {
  11. private Button Button1;
  12. private ImageView image1;
  13. private TextView text1;
  14. //参数初始化
  15. @SuppressLint("WrongViewCast")
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. Button1=findViewById(R.id.Button1);//寻找布局文件里ID与自己定义的绑定
  21. Button1.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View v) {
  24. //打印
  25. System.out.println("Hello!");
  26. //弹窗
  27. Toast.makeText(MainActivity.this,"hello",Toast.LENGTH_LONG).show();
  28. }
  29. });
  30. image1=findViewById(R.id.image1);
  31. image1.setOnClickListener(new View.OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. //弹窗
  35. Toast.makeText(MainActivity.this,"This a image!",Toast.LENGTH_LONG).show();
  36. }
  37. });
  38. text1=findViewById(R.id.text1);
  39. text1.setText("你好,2020年3月29日20:18:45");
  40. }
  41. }

发表评论

表情:
评论列表 (有 0 条评论,130人围观)

还没有评论,来说两句吧...

相关阅读

    相关 监听按钮事件

    3中注册监听方式比较 1.匿名内部类 需要获取控件对象,使用变量不方便;适用于单个事件 2.实现接口 需要获取控件对象,使用变量方便;适用于于多个事件 3.设置onC

    相关 动态绑定按钮事件

    在做asp.net网站时对表格数据(操作列)进行操作(编辑和添加),制作的弹出框一模一样,只是单击事件执行代码不一样,为了不重新制作窗体,只能动态改变a标签事件。