目录

HuaweiIdAuthButton控件使用指导

概述

HuaweiIdAuthButton是华为帐号提供的一个按钮控件,此控件展示华为风格的登录按钮,可以让您方便快速地实现符合华为帐号登录图标使用规范的登录按钮。此控件只处理按钮的显示效果,如果要触发登录动作,请使用setOnClickListener(OnClickListener)注册一个监听器。

支持的设备

设备类型

OS版本

HMS Core(APK)版本

手机、平板

EMUI 3.0及以上、Android 4.4及以上

4.0.0.300及以上

开发准备

按开发准备中的步骤配置AppGallery Connect并集成HMS Core SDK之后即可使用HuaweiIdAuthButton。

HuaweiIdAuthButton要求HMS Core SDK版本为4.0.0.300及以上。

使用指导

HuaweiIdAuthButton和一般的控件一样,有两种使用方式,一种是在布局xml文件中进行声明,另外一种是在Java或Kotlin代码中创建HuaweiIdAuthButton类的实例,并动态添加到布局中,您可以根据自己的需要选择使用。

[h2]通过布局xml文件使用

控件声明

首先按以下方式声明HuaweiIdAuthButton控件:

<com.huawei.hms.support.hwid.ui.HuaweiIdAuthButton
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
/>

界面示例

按钮文本请使用“华为帐号登录”,不可随意更改。

在布局文件中声明控件后,还需要在Java或Kotlin代码中使用setOnClickListener(OnClickListener)注册一个监听器,在监听事件中执行登录操作。

自定义参数

HuaweiIdAuthButton提供了以下几种自定义样式参数:

自定义参数名

可选值

说明

hwid_button_theme

hwid_button_theme_full_title

按钮展示为图标+文字。

hwid_button_theme_no_title

按钮不展示文字。

hwid_color_policy

hwid_color_policy_black

按钮颜色风格为黑色。

hwid_color_policy_gray

按钮颜色风格为灰色,只有hwid_button_theme= hwid_button_theme_full_title时可用。

hwid_color_policy_red

按钮颜色风格为红色。

hwid_color_policy_white

按钮颜色风格为白色。

hwid_color_policy_white_with_border

按钮颜色风格为带描边白色,只有hwid_button_theme= hwid_button_theme_full_title时可用。

hwid_corner_radius

hwid_corner_radius_large

按钮圆角设置,大圆角。

hwid_corner_radius_medium

按钮圆角设置,中等圆角。

hwid_corner_radius_small

按钮圆角设置,小圆角。

在布局文件引用命名空间:

xmlns:app="http://schemas.android.com/apk/res-auto"

自定义参数使用示例:

<com.huawei.hms.support.hwid.ui.HuaweiIdAuthButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:hwid_button_theme="hwid_button_theme_full_title"
    app:hwid_color_policy="hwid_color_policy_red"
    app:hwid_corner_radius="hwid_corner_radius_large"
/>

界面示例

按钮文本请使用“华为帐号登录”,不可随意更改。

[h2]通过Java或Kotlin代码创建实例

控件声明

通过HuaweiIdAuthButton的构造函数可以创建对应实例,并通过addView的方式把HuaweiIdAuthButton加入到布局中。

构造函数

说明

HuaweiIdAuthButton(Context context)

构造方法,创建HuaweiIdAuthButton实例。

HuaweiIdAuthButton(Context context, AttributeSet attributeSet)

构造方法,创建HuaweiIdAuthButton实例,根据属性集加载样式。

HuaweiIdAuthButton(Context context, AttributeSet attributeSet, int defStyleAttr)

构造方法,创建HuaweiIdAuthButton实例,可以指定默认样式。

示例代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_sign_in_api"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
</LinearLayout>
“Java”
HuaweiIdAuthButton huaweiIdAuthButton = new HuaweiIdAuthButton(this);
LinearLayout layout = findViewById(R.id.layout_sign_in_api);
layout.addView(huaweiIdAuthButton);
“Kotlin”
val huaweiIdAuthButton = HuaweiIdAuthButton(this)
val layout = findViewById<LinearLayout>(R.id.layout_sign_in_api)
layout.addView(huaweiIdAuthButton)

自定义参数

HuaweiIdAuthButton提供了以下几种自定义样式参数:

自定义参数方法

可选值

说明

setTheme(int theme)

HuaweiIdAuthButton.THEME_FULL_TITLE

按钮主题为展示图标+标题。

HuaweiIdAuthButton.THEME_NO_TITLE

按钮主题为不展示标题。

setColorPolicy(int colorPolicy)

HuaweiIdAuthButton.COLOR_POLICY_BLACK

按钮颜色风格为黑色。

HuaweiIdAuthButton.COLOR_POLICY_GRAY

按钮颜色风格为灰色,只有主题为THEME_FULL_TITLE时可用。

HuaweiIdAuthButton.COLOR_POLICY_RED

按钮颜色风格为红色。

HuaweiIdAuthButton.COLOR_POLICY_WHITE

按钮颜色风格为白色。

HuaweiIdAuthButton.COLOR_POLICY_WHITE_WITH_BORDER

按钮颜色风格为带描边白色,只有主题为THEME_FULL_TITLE时可用。

setCornerRadius(int cornerRadiusPx)

HuaweiIdAuthButton.CORNER_RADIUS_LARGE

按钮圆角设置,大圆角。

HuaweiIdAuthButton.CORNER_RADIUS_MEDIUM

按钮圆角设置,中等圆角。

HuaweiIdAuthButton.CORNER_RADIUS_SMALL

按钮圆角设置,小圆角。

setUIMode(int theme, int colorPolicy, int cornerRadius)

参考以上theme,colorPolicy,cornerRadius的可选值

设置HuaweiIdAuthButton的主题、颜色风格和圆角半径。

setEnabled(boolean flag)

true/false

设置是否可以点击。

示例代码

“Java”
HuaweiIdAuthButton huaweiIdAuthButton = new HuaweiIdAuthButton(this);
huaweiIdAuthButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // 进行登录的操作处理
    }
});
huaweiIdAuthButton.setTheme(HuaweiIdAuthButton.THEME_FULL_TITLE);
huaweiIdAuthButton.setColorPolicy(HuaweiIdAuthButton.COLOR_POLICY_RED);
huaweiIdAuthButton.setCornerRadius(HuaweiIdAuthButton.CORNER_RADIUS_LARGE);
LinearLayout layout = findViewById(R.id.layout_sign_in_api);
layout.addView(huaweiIdAuthButton);
“Kotlin”
val huaweiIdAuthButton = HuaweiIdAuthButton(this)
huaweiIdAuthButton.setOnClickListener {
    // 进行登录的操作处理
}
huaweiIdAuthButton.theme = HuaweiIdAuthButton.THEME_FULL_TITLE
huaweiIdAuthButton.colorPolicy = HuaweiIdAuthButton.COLOR_POLICY_RED
huaweiIdAuthButton.cornerRadius = HuaweiIdAuthButton.CORNER_RADIUS_LARGE
val layout = findViewById<LinearLayout>(R.id.layout_sign_in_api)
layout.addView(huaweiIdAuthButton)