加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml输入是如传递给java的

发布时间:2020-12-16 09:04:59 所属栏目:百科 来源:网络整理
导读:废话叫前言 开发语言的进步,感触最新的就是,以前写windows的时候使用的是C#后来,学习了一下WPF,内心就是感慨万千 居然用xml来写界面了,不用那些抽象的代码来写,关键是xml的界面还用实时的可观性。xml就和html一样,标记语言。 可以直接在浏览器看到。
    废话叫前言


开发语言的进步,感触最新的就是,以前写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;

			@Override
			public 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();
						}
					}
				}
			}
		});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读