【Java UI】HarmonyOs如何集成ButterKnife
作者:互联网
参考资料
参考 ButterKnife
项目配置
在项目级添加如下代码(效果如下)
mavenCentral()
在应用级添加如下代码如下图所示
implementation 'io.openharmony.tpc.thirdlib:butterKnife:1.0.2'
annotationProcessor 'io.openharmony.tpc.thirdlib:butterknife-compiler:1.0.2'
代码实现
- 绘画xml布局代码和效果图如下,id分别取值为text_click,text_Result
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Text
ohos:id="$+id:text_click"
ohos:height="100vp"
ohos:width="match_parent"
ohos:text="点击事件一"
ohos:background_element="#ed6262"
ohos:text_size="40vp"
ohos:text_alignment="center"/>
<Text
ohos:id="$+id:text_click_two"
ohos:height="100vp"
ohos:width="match_parent"
ohos:text="点击事二"
ohos:text_size="40vp"
ohos:text_alignment="center"/>
<Text
ohos:id="$+id:text_Result"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#ed6262"
ohos:layout_alignment="horizontal_center"
ohos:text_size="20vp"
ohos:text="点击效果"
/>
</DirectionalLayout>
- 在abilitySlice重写OnStart的方法代码如下
@Override
public void onStart(Intent intent) {
super.onStart(intent);
ComponentContainer mRootLayout = (ComponentContainer) LayoutScatter.getInstance(MainAbilitySlice.this).
parse(ResourceTable.Layout_ability_main, null, false);
ButterKnife.bind(this, mRootLayout);
super.setUIContent(mRootLayout);
}
注解查找控件代码如下
@BindComponent(ResourceTable.Id_text_Result)
Text mTextResult;
注解实现点击事件代码如下
/**
* 可以在OnClick的{}是一个数组结合可以实现好多组件事件的绑定
* 例如 @OnClick({ResourceTable.Id_text_click,ResourceTable.xxx})
* 如何判断是哪一个点击事件
* 可以根据Component的getId的方法获取到组件id,然后实现点击事件
* @param view
*/
@OnClick({ResourceTable.Id_text_click,ResourceTable.Id_text_click_two})
public void click(Component view){
switch (view.getId()){//todo 获取组件的id
case ResourceTable.Id_text_click://todo case 判定是一个组件事件
mTextResult.setText("触发了点击事件一");
break;
case ResourceTable.Id_text_click_two:
mTextResult.setText("触发了点击事件二");
break;
}
}
运行效果
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh
标签:Java,text,代码,Id,HarmonyOs,ButterKnife,ResourceTable,如下,click 来源: https://www.cnblogs.com/developer-huawei/p/16560530.html