zoukankan      html  css  js  c++  java
  • Android学习之六种事件响应方法汇总

    java源码如下:

    1、MainActivity.java源码

      1 package com.example.responsetest;
      2 import android.app.Activity;
      3 import android.content.Intent;
      4 import android.content.Loader.ForceLoadContentObserver;
      5 import android.graphics.Color;
      6 import android.os.Bundle;
      7 import android.view.Menu;
      8 import android.view.MenuItem;
      9 import android.view.View;
     10 import android.view.View.OnClickListener;
     11 import android.widget.Button;
     12 import android.widget.ImageButton;
     13 import android.widget.TextView;
     14 
     15 //方法六:通用场景,特别是某个控件的点击方法需要实现的逻辑较为复杂的时候
     16 public class MainActivity extends Activity{
     17     TextView textview;
     18     @Override
     19     protected void onCreate(Bundle savedInstanceState) {
     20         super.onCreate(savedInstanceState);
     21         setContentView(R.layout.activity_main);
     22         textview=(TextView)findViewById(R.id.textview);//1.初始化值
     23     }
     24     public void onTextViewClick(View view) { //2.传入形参
     25         //跳转页面
     26         Intent intent=new Intent(MainActivity.this,SecondActivity.class);
     27         startActivity(intent);
     28         
     29         //textview.setTextColor(Color.GREEN);//点击变色
     30     }
     31 }
     32 
     33 
     34 /*
     35 //方法五:适用于不同类型的控件,使需要实现的功能在一个onClick()方法中实现
     36 public class MainActivity extends Activity implements OnClickListener{
     37     @Override
     38     protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40         setContentView(R.layout.activity_main);
     41         Button button1=(Button)findViewById(R.id.button1);
     42         Button button2=(Button)findViewById(R.id.button2);
     43         ImageButton button3=(ImageButton)findViewById(R.id.imagebutton);
     44         button1.setOnClickListener(this);
     45         button2.setOnClickListener(this);
     46         button3.setOnClickListener(this);
     47     }
     48     
     49     @Override
     50     public void onClick(View arg0) {
     51         switch (arg0.getId()) {
     52         case R.id.button1:
     53         case R.id.button2:
     54         case R.id.imagebutton:
     55             Intent intent=new Intent(MainActivity.this,SecondActivity.class);
     56             startActivity(intent);
     57             break;
     58         default:
     59             break;
     60         }
     61     }
     62 }
     63 */
     64 
     65 /*
     66 //方法四:与方法三差别不大:
     67 public class MainActivity extends Activity{
     68     @Override
     69     protected void onCreate(Bundle savedInstanceState) {
     70         super.onCreate(savedInstanceState);
     71         setContentView(R.layout.activity_main);
     72         Button button1=(Button)findViewById(R.id.button1);
     73         Button button2=(Button)findViewById(R.id.button2);
     74         button1.setOnClickListener(new MyOnClick());
     75         button2.setOnClickListener(new MyOnClick());
     76     }
     77     private class MyOnClick implements OnClickListener{
     78         @Override
     79         public void onClick(View arg0) {
     80             switch (arg0.getId()) {
     81             case R.id.button1:
     82             case R.id.button2:
     83                 Intent intent=new Intent(MainActivity.this,SecondActivity.class);
     84                 startActivity(intent);
     85                 break;
     86             default:
     87                 break;
     88             }    
     89         }
     90     }
     91 }
     92 */
     93 /*
     94 //方法三:适用于多个同类型的控件(比如Button)等
     95 public class MainActivity extends Activity{    
     96     Button[] buttons;//定义一个Button数组,封装多个同类型的Button控件(成员变量有默认的初始值)
     97     //初始化函数
     98     @Override
     99     protected void onCreate(Bundle savedInstanceState){
    100         super.onCreate(savedInstanceState);
    101         setContentView(R.layout.activity_main);
    102         buttons=new Button[]{(Button)findViewById(R.id.button1),(Button)findViewById(R.id.button2)};
    103         for(Button button:buttons) {
    104             button.setOnClickListener(listener);
    105         }}
    106     //成员变量:
    107     private OnClickListener listener=new OnClickListener() {
    108         @Override
    109         public void onClick(View arg0) {
    110             switch (arg0.getId()) {
    111             case R.id.button1:
    112             case R.id.button2:
    113                 Intent intent=new Intent(MainActivity.this,SecondActivity.class);
    114                 startActivity(intent);
    115                 break;
    116             default:
    117                 break;
    118             }}};
    119 }
    120 */
    121 
    122 /*
    123 
    124 public class MainActivity extends Activity {
    125     @Override
    126     protected void onCreate(Bundle savedInstanceState) {
    127         super.onCreate(savedInstanceState);
    128         setContentView(R.layout.activity_main);
    129         Button button=(Button)findViewById(R.id.button2);
    130         button.setOnClickListener(new MyOnclickListener());
    131     }
    132     //事件响应方法二:定义内部类实现监听接口
    133     public class MyOnclickListener implements OnClickListener{
    134         @Override
    135         public void onClick(View arg0) {
    136             Intent intent=new Intent(MainActivity.this,SecondActivity.class);
    137             startActivity(intent);
    138         }
    139     }
    140 }
    141 
    142 */
    143 /*
    144 public class MainActivity extends Activity {
    145     @Override
    146     protected void onCreate(Bundle savedInstanceState) {
    147         super.onCreate(savedInstanceState);
    148         setContentView(R.layout.activity_main);
    149         
    150         //事件响应方法一:使用匿名内部类,适用于任何场景        
    151         findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
    152             @Override
    153             public void onClick(View v) {
    154                 Intent intent=new Intent(MainActivity.this,SecondActivity.class);
    155                 startActivity(intent);
    156             }
    157         });;
    158     }
    159 }
    160 */

     2、SecondActivity.java源码

     1 package com.example.responsetest;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 
     6 public class SecondActivity extends Activity{
     7     @Override
     8     protected void onCreate(Bundle savedInstanceState) {
     9         super.onCreate(savedInstanceState);
    10         setContentView(R.layout.activity_second);
    11     }
    12 }

    xml代码如下:

    1、activity_main.xml代码(第一界面):

    1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:id="@+id/main_activity"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:orientation="vertical"
     7     android:paddingBottom="@dimen/activity_vertical_margin"
     8     android:paddingLeft="@dimen/activity_horizontal_margin"
     9     android:paddingRight="@dimen/activity_horizontal_margin"
    10     android:paddingTop="@dimen/activity_vertical_margin"
    11     tools:context="com.example.responsetest.MainActivity" >
    12 
    13     <Button
    14         android:id="@+id/button1"
    15         android:layout_width="match_parent"
    16         android:layout_height="wrap_content"
    17         android:text="匿名内部类" />
    18     
    19      <Button 
    20         android:id="@+id/button2"
    21         android:layout_width="match_parent"
    22         android:layout_height="wrap_content"
    23         android:text="内部类"/>
    24      <ImageButton 
    25          android:id="@+id/imagebutton"
    26          android:layout_width="100dip"
    27          android:layout_height="100dip"
    28          android:scaleType="fitCenter"
    29          android:src="@drawable/ic_launcher"
    30          />
    31      <TextView 
    32          android:id="@+id/textview"
    33          android:layout_width="match_parent"
    34          android:layout_height="wrap_content"
    35          android:onClick="onTextViewClick"
    36          android:text="点击事件"
    37          android:textSize="50sp"
    38          />
    39 
    40 </LinearLayout>

    2、activity_second.xml源码(第二界面):

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context="com.example.responsetest.MainActivity" >
    10 
    11     <TextView
    12         android:id="@+id/secondactivity"
    13         android:layout_width="match_parent"
    14         android:layout_height="wrap_content"
    15         android:text="欢迎您" />
    16 
    17 </LinearLayout>

    3、AndroidManifest.xml源码:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.example.responsetest"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6 
     7     <uses-sdk
     8         android:minSdkVersion="18"
     9         android:targetSdkVersion="22" />
    10 
    11     <application
    12         android:allowBackup="true"
    13         android:icon="@drawable/ic_launcher"
    14         android:label="@string/app_name"
    15         android:theme="@style/AppTheme" >
    16         <activity
    17             android:name=".MainActivity"
    18             android:label="@string/app_name" >
    19             <intent-filter>
    20                 <action android:name="android.intent.action.MAIN" />
    21 
    22                 <category android:name="android.intent.category.LAUNCHER" />
    23             </intent-filter>
    24         </activity>
    25         <activity
    26             android:name="com.example.responsetest.SecondActivity"
    27             android:excludeFromRecents="true"
    28             android:noHistory="true"/>
    29     </application>
    30 
    31 </manifest>

     代码分析:

    1.两个java类:

      MainActivity.java :主类,代码的主要执行者,操作第一界面

      SecondActivity.java:跳转后的页面,操作第二界面

    2.三个xml文件:

    activity_main.xml:布局第一个手机界面

    activity_second.xml:布局第二个界面(跳转后的界面)

    AndroidManifest.xml:Android代码的执行入口

  • 相关阅读:
    2020牛客暑期多校训练营(第三场)
    2020牛客暑期多校训练营(第二场)
    C# 获取枚举类型中所有描述信息 Xinner
    XPath Xinner
    02kubeadm安装
    thinkphp6:访问redis6(thinkphp 6.0.9/php 8.0.14)
    npm8.3.0 安装@vue/cli 4.5.15
    linux(ubuntu 21.10): php8.0.14:安装phpredis以访问redis(phpredis5.3.5)
    linux(ubuntu21.10):apt方式安装redis6.0.15
    阿里云何万青:南坡VS北坡,阿里云高性能计算行业实践
  • 原文地址:https://www.cnblogs.com/hh8888-log/p/9762361.html
Copyright © 2011-2022 走看看