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
原创粉丝点击