众所周知,安卓应用开发经过这么多年的发展相对成熟和稳定,鸿蒙 OS 作为后来者兼容一个成熟的开发体系会节省很多推广和开发成本。
但在实际开发中,代码层面仍然有很多细节上的差异,会给初次开发人员造成困扰。
本宝典旨在汇总实际开发中第三方件接入时的代码差异,以及帮助开发人员更好的进行开发作业,由于目前接触的开发类型有限,所汇总的内容多少会有疏漏,后期我们会进一步完善和补全。
getWindowManager().getDefaultDisplay();
Optional
display = DisplayManager.getInstance().getDefaultDisplay(this.getContext());
Point pt = new Point();
display.get().getSize(pt);
安卓:
略。
""metaData"":{
""customizeData"":[
{
""name"": ""hwc-theme"",
""value"": ""androidhwext:style/Theme.Emui.NoTitleBar"",
""extra"":""""
}
]
}
Resources.getSystem().getDisplayMetrics().density
// 获取屏幕密度
Optional
display = DisplayManager.getInstance().getDefaultDisplay(this.getContext());
DisplayAttributes displayAttributes = display.get().getAttributes();
//displayAttributes.xDpi;
//displayAttributes.yDpi;
安卓:
android.view.View; class ProgressBar extends View
class ProgressBar extends Component
安卓:略。
鸿蒙:两种方式。
A:在config.json ability 中添加。
"metaData"": {
""customizeData"": [
{
""extra"": """",
""name"": ""hwc-theme"",
""value"
": ""androidhwext:style/Theme.Emui.Light.NoTitleBar""
}
]
}
B:在 AbilitySlice 的 onStart 函数内增加如下代码,注意要在 setUIContent 之前。
getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
安卓:
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1)
requestPermissionsFromUser(
new String[]{""ohos.permission.READ_MEDIA"", ""ohos.permission.WRITE_MEDIA"", ""ohos.permission.READ_USER_STORAGE"", ""ohos.permission.WRITE_USER_STORAGE"",}, 1);
安卓:
A.从 A 跳转至 B,没有参数,并且不接收返回值。
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivity(intent);
Intent intent = new Intent(this, B.class);
intent.putExtra(""name"", ""lily"");
startActivity(intent);
Intent intent = new Intent(this, B.class);
intent.putExtra(""name"", ""lily"");
startActivityForResult(intent, 2);
鸿蒙:
A.从 A 跳转至 B,没有参数,并且不接收返回值。
present(new BSlice(), new Intent());
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId("""") .withBundleName(""com.test"") .withAbilityName(""com.test.BAbility"")
.build();
intent.setParam(""name"",""lily"");
intent.setOperation(operation);
startAbility(intent);
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId("""") .withBundleName(""com.test"") .withAbilityName(""com.test.BAbility"")
.build();
intent.setParam(""name"",""lily"");
intent.setOperation(operation);
startAbilityForResult(intent,100);
安卓:
"".B"">
<intent-filter>
<action android:name=""com.hly.view.fling""/>
intent-filter>
activity>
Intent intent = new Intent(); intent.setAction(""com.hly.view.fling""); intent.putExtra(""key"", ""name""); startActivity(intent);
鸿蒙:
A.在 config.json 文件 ability 中添加以下信息
"skills"":[
{
""actions"":[
""ability.intent.gotopage""
]
}
]
B.在 MainAbility 的 onStart 函数中,增加页面路由
addActionRoute( ""ability.intent.gotopage"", BSlice.class.getName());
Intent intent = new Intent();
intent.setAction(""ability.intent.gotopage"");
startAbility(intent);
安卓:Fragment。
鸿蒙:Fraction。
A:Ability 继承 FractionAbility
getFractionManager().startFractionScheduler()
C:构造 Fraction
FractionScheduler.add()
FractionScheduler.remove()
FractionScheduler.replace()
https://www.jianshu.com/p/58558dc6673a
安卓:
LayoutInflater.from(mContext).inflate(R.layout.banner_viewpager_layout, null);
鸿蒙:
LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_ability_main, null, false);
ImageView.setImageDrawable(Drawable drawable);
并重写 Drawable 的 draw 函数。
Component.addDrawTask(Component.DrawTask task);
并实现 Component.DrawTask 接口的 onDraw 函数。
安卓:需要 3 步。
A.在 values/attrs.xml,在其中编写 styleable 和 item 等标签元素。
xmln:app= ""http://schemas.android.com/apk/res/-auto""
C.在自定义组件的构造函数中,调用 array.getInteger(R.styleable.***, 100); 获取属性。
鸿蒙:只需 2 步。
A. 在组件定义的 layout.xml 中增加:
xmlns:app=""http://schemas.huawei.com/apk/res/ohos""
然后就可以使用 app:***(*** 为任意字符串)来增加自定义属性了,为了区分建议加上组件名前缀。
B. 在自定义组件的带 AttrSet 参数的构造函数中,使用下面代码获取属性。
attrSet.getAttr(""***"").get().getStringValue();
ohos.multimodalinput.event.TouchEvent
ohos.eventhandler.EventHandler
android.view.View.OnTouchListener
ohos.agp.components.Component.
TouchEventListener
安卓:
implements PageSlider.PageChangedListener
Implements OnPageChangedListener
安卓:直接重写 onTouchEvent。
鸿蒙:继承 Component.TouchEventListener,然后在构造方法中设置监听 setTouchEventListener(this::onTouchEvent); 实现 onTouchEvent。
event.getX(), event.getY()
MmiPoint point = touchEvent.getPointerPosition(touchEvent.getIndex());
setLineSpacingMultiplier(float f)
setSelectedNormalTextMarginRatio(float f)
View.OnLayoutChangeListener
Component.LayoutRefreshedListener
安卓:
安卓:
extends RecyclerView.Adapter<>
extends RecycleItemProvider
Component.ComponentStateChangedListener
安卓:重写 onLayout(boolean changed, int left, int top, int right, int bottom)。
鸿蒙:重写 Component.LayoutRefreshedListener 的 onRefreshed 方法。
安卓:ListView。
鸿蒙:ListContainer。
setBackgroundColor(maskColor);
// 创建背景元素
ShapeElement shapeElement = new ShapeElement();
// 设置颜色
shapeElement.setRgbColor(new RgbColor(255, 0, 0));
view.setBackground(shapeElement);
可以在控件上、下、左、右设置图标,大小按比例自适应
setCompoundDrawablesWithIntrinsicBounds
RadioButton 组件在 xml 中如何设置 checked 属性
安卓:在 xml 中可以设置。
radioButton = findComponentById();
radioButton.setChecked(true);
备注:sdk 2.0 后 xml 中没有了 checked 属性,如果使用,可以在 java 代码中实现。
Component.ComponentStateChangedListener
为 ckeckbox 或者 Switch 按钮设置资源图片
安卓:略。
VectorElement vectorElement = new VectorElement(this, ResourceTable.Graphic_candy);
setBackground(vectorElement)
安卓:略。
鸿蒙:注册 setDraggedListener 侦听,实现 onDragPreAccept 方法,再方法内根据拖拽方向判断是否需要父组件处理,如果需要则返回 false,否则返回 true。