超级原创xxMix android:onClick上帝啊,不知道有没有人用过,我用了,在网上好多鸟回答乱七八糟,自己找到了
2011-11-30 13:38
363 查看
超级原创xxMix android:onClick上帝啊,不知道有没有人用过,我用了,在网上好多鸟回答乱七八糟,自己找到了
android:onClick = “xx”
public void (View v) 记得函数参数匹配 根据多态性以及函数指针原理这里的函数会注册给android:onClick的监听器
这样每个button都可以使用onClck定义自己测处理方法,也可以共用,但是关于事件源的区分就没有那么方便了,可能需要获取坐标才能逻辑上判断是哪个按钮或者根据
完整例子
main.xml
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:onClick = "xxx"
android:text="ok" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button
android:onClick = "xxx"
android:text="no" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
MainActivity.java
package com.sms;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainAcitivy extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void xxx(View v)
{
//可以直接转型但是注意有时候子类不匹配 ((Button)v).getText()
Button temp = (Button)v; //最好用instanceof判断子类型的实例,,注意子类型和子类不一样,面向对象设计原理
if(temp.getText().toString().trim().equalsIgnoreCase("ok"))
{
Toast.makeText(this,"ok ", Toast.LENGTH_SHORT).show();
}
if(temp.getText().toString().trim().equalsIgnoreCase("no"))
{
Toast.makeText(this,"no ", Toast.LENGTH_LONG).show();
}
}
public void xxxx(View v) //也可以单独映射自己的方法,但是单独写代码过于冗余 所以建议统一 使用getText判断仍然是好方法,需要记住处理函数参数
{
Toast.makeText(this,"xxxx", Toast.LENGTH_SHORT).show();
}
}
按钮上的文字区分事件源
android:onClick = “xx”
public void (View v) 记得函数参数匹配 根据多态性以及函数指针原理这里的函数会注册给android:onClick的监听器
这样每个button都可以使用onClck定义自己测处理方法,也可以共用,但是关于事件源的区分就没有那么方便了,可能需要获取坐标才能逻辑上判断是哪个按钮或者根据
完整例子
main.xml
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:onClick = "xxx"
android:text="ok" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button
android:onClick = "xxx"
android:text="no" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
MainActivity.java
package com.sms;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainAcitivy extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void xxx(View v)
{
//可以直接转型但是注意有时候子类不匹配 ((Button)v).getText()
Button temp = (Button)v; //最好用instanceof判断子类型的实例,,注意子类型和子类不一样,面向对象设计原理
if(temp.getText().toString().trim().equalsIgnoreCase("ok"))
{
Toast.makeText(this,"ok ", Toast.LENGTH_SHORT).show();
}
if(temp.getText().toString().trim().equalsIgnoreCase("no"))
{
Toast.makeText(this,"no ", Toast.LENGTH_LONG).show();
}
}
public void xxxx(View v) //也可以单独映射自己的方法,但是单独写代码过于冗余 所以建议统一 使用getText判断仍然是好方法,需要记住处理函数参数
{
Toast.makeText(this,"xxxx", Toast.LENGTH_SHORT).show();
}
}
按钮上的文字区分事件源
相关文章推荐
- 使用了博客园的备份后得到一个xml文件,不知道有没有css或者xslt文件可以在本地阅读文章,在网上找了半天也没有找到解决办法,难道要自己写?
- Android JNI学习之javah命令的正确使用(找了好半天才找到的,汉,网上好多说法都没用)
- (转载)Android JNI学习之javah命令的正确使用(找了好半天才找到的,汉,网上好多说法都没用)
- Android JNI学习之javah命令的正确使用(找了好半天才找到的,汉,网上好多说法都没用)
- android 中 webview 怎么用 localStorage? 我在 android里面 使用html5的 localStorage 为什么存不进去也读不出来呀? 网上搜了好多都没效果
- Android studio代码模板(找到自己需要的添加到代码模板里就可以)--- 能装逼又实用
- 【原创】Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- Android中Cursor类(自己将网上搜索到的的整合再次编写了一下)
- 今天找到了第一份android工作,说说自己的心情,希望一个月之后我能坚持下来 真正的入行
- svn更新代码的时候出现cleanup的问题,网上找到了答案,我是解决了,不知道其他版本适不适用!!!
- 【原创】密码升级:超级可变、抗记录、自己易记的密码
- 从网上找到的一些android开发时所要用到的权限
- 虚拟机联网的三种原理-网上找到的,自己插入图方便理解
- ios如何集成百度地图---还是自己动手做一下--网上好多博客说的都不甚靠谱。
- 分散處理將是未來的 mud 必需具備的能力 (转载:不知道自己从那里找到的:))
- 【android乱七八糟】关于OnClickListener事件中加switch语句讨论
- Android手机调查1G以上的(网上找到很有价值)
- 什么是补码-网上找到的,非原创
- 关于android 消息提醒,在网上找到这个项目在eslipse中使用ValueAnimator报错
- Java中Process的stdout, stderr的重定向,自己想的,不知道有没有更好的办法