问个基础问题,如何获取当前组件文字颜色呢?

问个基础问题,如何获取当前组件文字颜色呢?

阅读 171
1 个回答
import com.ohos.agp.components.Text;
import com.ohos.agp.components.ComponentContainer;
import com.ohos.agp.utils.Color;

public class TextColorAbility extends Ability {
    @Override
    public void onStart(Intent want) {
        super.onStart(want);
        super.setUIContent(createLayout());
    }

    private ComponentContainer createLayout() {
        Text text = new Text(getContext());
        text.setText("Hello World");
        text.setTextColor(Color.RED);

        // 添加点击事件来获取文字颜色
        text.setClickedListener(component -> {
            Color textColor = text.getTextColor();
            Log.info("Text color: " + textColor.toHexString());
        });

        return new ColumnLayout(getContext(), new Component[]{text});
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题