xml输入点击事件是如传递给java的
来源:互联网 发布:uipath软件 编辑:程序博客网 时间:2024/06/04 19:16
开发语言的进步,感触最新的就是,以前写windows的时候使用的是C#后来,学习了一下WPF,内心就是感慨万千
居然用xml来写界面了,不用那些抽象的代码来写,关键是xml的界面具有实时性、可观性。xml就和html一样,标记语言。
可以直接在浏览器看到。
之后学习Android后,感觉又回来,真的,语言在进步呀。
在Android中xml时如何连接到java的呢?
是这样的:
value中新建一个atrrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DivActionBar">
<attr name="right_onclick" format="string" />
</declare-styleable>
</resources>
xml中引用:
使用空间名:
xmlns:app=”http://schemas.android.com/apk/res/com.tuxiaobei“
使用
app:right_onclick="function"
java中映射关联:
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.DivActionBar);final String methodName;</span><span style="font-size:18px;">methodName = array.getString(R.styleable.DivActionBar_right_onclick);</span></strong>rightText.setOnClickListener(new OnClickListener() {private Method method;@Overridepublic void onClick(View v) {if (!TextUtils.isEmpty(methodName)) {if (method == null) {try {method = getContext().getClass().getMethod(methodName,DivActionBar.class);} catch (NoSuchMethodException e) {throw new IllegalStateException("Could not find a method "+ method + "(View) in the activity "+ getContext().getClass() + " for onClick handler"+ " on view " + DivActionBar.this.getClass() + e);}try {method.invoke(getContext(), DivActionBar.this);} catch (IllegalAccessException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}}}});
关键是:method = getContext().getClass().getMethod(methodName,DivActionBar.class)
对于项目的还是不要在xml中写点击事件,毕竟是有点性能嫌疑滴。
0 0
- xml输入点击事件是如传递给java的
- 点击事件是如何传递给Activity的
- 点击事件的传递
- android点击事件的传递
- 事件响应链,点击屏幕时是如何互动的,事件的传递
- 输入事件的传递过程
- 输入事件的传递过程
- 输入事件的传递过程
- 在Layout响应点击事件但不传递给内部的childView
- 点击事件是特殊的触摸事件
- Fragment的点击事件的传递
- android...View的点击事件传递详解,
- Button点击时滑动的事件传递
- 给android的LinearLayout添加点击事件
- 给android的LinearLayout添加点击事件
- 给android的LinearLayout添加点击事件
- 给Android的LinearLayout添加点击事件
- 给android的LinearLayout添加点击事件
- 三种方式使得iOS应用能够在后台进行数据更新和下载
- OpenCV-Python教程(8、Canny边缘检测)
- 取消百度SiteApp转码
- Android 编程下 App Install Location
- OpenCV-Python教程(9、使用霍夫变换检测直线)
- xml输入点击事件是如传递给java的
- 是什么时候开始发现,你在教父母东西了
- android studio 删除、导入jar包
- OpenCV-Python教程(10、直方图均衡化)
- Android多种方式实现自定义Dialog对话框
- C++建议
- 用网页服务器实现钢琴弹奏(使用Wizwiki-W7500)
- GRID汇总
- OpenCV-Python教程(11、轮廓检测)