您的位置:首页 > 移动开发 > Android开发

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

2010-02-11 15:13 591 查看
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
Button, CheckedTextView, Chronometer, DigitalClock, EditText



Known Indirect Subclasses
AutoCompleteTextView, CheckBox, CompoundButton, ExtractEditText, MultiAutoCompleteTextView, RadioButton, ToggleButton

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

boolean isClicked =false;
@Override public void onClick(View v) {
if( vinstanceof 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) {
if( vinstanceof 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做监听器。
}

}

运行效果





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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: