Android 组件学习【启发】 如何处理好多个组件的多个事件

Android 组件学习【启发】 如何处理好多个组件的多个事件

今天无意间看了一篇文章

http://blog.csdn.net/vagrxie/archive/2009/08/04/4409464.aspx

感觉这篇文章给我较深的印象。他的学习思路:看组件的直接子类或者父类,利用面型对象的思想来研究组件。

比如下面是TextView组件的 继承关系。

TextView

extends View
implements ViewTreeObserver.OnPreDrawListener

java.lang.Object

   ↳

android.view.View

 

   ↳

android.widget.TextView

Known Direct Subclasses 

ButtonCheckedTextViewChronometerDigitalClockEditText

Known Indirect Subclasses 

AutoCompleteTextViewCheckBoxCompoundButtonExtractEditTextMultiAutoCompleteTextViewRadioButtonToggleButton

下面是他实现的 点击每一个按钮的代码

 boolean isClicked =false;

    @Override public void onClick(View v) {

        ifvinstanceof Button) {

            if(!isClicked) {

                ((Button)v).setText("Hello, New Button.");

                isClicked =true;

            }

            else 

            {

                ((Button)v).setText("Hello, Old Button.");

                isClicked =false;

            }

        }

    }

可以看见 ,他没有些监听函数 ,直接给每一个按钮 添加 的 监听的事件是 this 就是当前的对象。也没有繁琐的代码说明 我点击的是哪一个按钮,其实 ,我们可以 在程序里面添加 判断 自己i是点击的哪一个按钮的,这样子 就不用自己 写老多的代码了。

@Override public void onClick(View v) {

        ifvinstanceof Button) {

            if(v.getId()=R.id.A) { //这里的A使我们的按钮ID要有 ,这里我做实验随便写的

                ((Button)v).setText("A按钮 点击了 .");

                //添加 ID为A的按钮要实现的代码

            }

            else  if(v.getId()==R.id.B)

            {

                ((Button)v).setText("B按钮 点击了 .");

                //添加 ID为B的按钮要实现的代码

            }

        }

}

可以看出 我们可以扩展好多 的。。。 就写到这里吧 ! 感觉挺好的。这个方法

如果不用的话 我们就要繁琐的给每一个按钮写好多东西 还乱。

This class represents the basic building block for user interface components. A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). 

说明View Widget的基类。

运用这个方法代码有条里了,我么有用之前代码

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

//        btnListener=new OnClickListener(){

//          public void  onClick(View V){

//          Intent btnIntent=new Intent(StartMain.this,button.class);

//          startActivity(btnIntent);

//          }

//         };

//         textViewListener=new OnClickListener(){

//          public void onClick(View v){

//          Button textButton=(Button)findViewById(R.id.btnTextViw);

//  

//          Intent textViwIntent=new Intent(StartMain.this,TextView.class);

//          startActivity(textViwIntent); 

//          }

//         };

//         setContentView(R.layout.main);

//         Button  btnButton=(Button)findViewById(R.id.btnButtonDemo);

//         btnButton.setOnClickListener(btnListener);

  }

记得哦 我这里 监听器是匿名内部类。下面 的 要实现  OnclickListener 

public class StartMain extends Activity  implements OnClickListener

只有这样 this 才可以做监听器的。

下面是我新实现的代码., 感觉比较简洁、工整。

package com.fzxy.zxy;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class StartMain extends Activity   implements OnClickListener {

OnClickListener btnListener = null;

OnClickListener textViewListener = null;

 public void onClick(View v) {

 //这里 也可以判断其他组件的事件

 if(v instanceof Button){

 Button tempBtn=(Button)v;

 if(tempBtn.getId()==R.id.btnTextViw){

 Intent textViwIntent=new Intent(StartMain.this,textViewText.class);

 startActivity(textViwIntent);

 }

 else if(tempBtn.getId()==R.id.btnButtonDemo){

 Intent btnIntent=new Intent(StartMain.this,button.class);

 startActivity(btnIntent);

 }

 }

 }

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button demoBtn = (Button) findViewById(R.id.btnButtonDemo);

Button textviewBtn = (Button) findViewById(R.id.btnTextViw);

  textviewBtn.setOnClickListener(this);

  demoBtn.setOnClickListener(this);

// 这里如果还有其他的组件,可以仿照类似的,添加监听器,这里由于事先了 OnClickListener 可以用This做监听器。

}

}

运行效果

向下时点击的时候的运行结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值