Introduction
In this article, we can create a small number game with below features:
1. User taps on start, two random generated numbers are shown on screen and user has to guess sum of those numbers within 10 seconds.
2. Timer will be shown for 10 seconds, after 10 seconds result will be displayed.
3. Bottom of screen change level buttons are available to increase the complexity of game.
Requirements
1. DevEco IDE
2. Lite wearable watch (Simulator)
Game Flow
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
UI Design
index.hml
HTML:
<div class="container">
<text class="game_title">Guess Right Number</text>
<div class="sub_container">
<text class="small_title">{{number1}}</text>
<text class="small_title">+
</text>
<text class="small_title">{{number2}}</text>
</div>
<text class="text_button" onclick="generateNewNumbers">START</text>
<text class="total">{{total}}</text>
<progress class="progress_horizontal" type="horizontal" percent="{{progressNumber}}"></progress>
<div class="below_half">
<text class="settings">Change Level</text>
<div if="{{defaultLevel == 100}}" class="sub_container">
<text id="1" class="single_char" onclick="switchLevel('1')">1</text>
<text id="10" class="selected_single_char" onclick="switchLevel('10')">2</text>
<text id="100" class="single_char" onclick="switchLevel('100')">3</text>
</div>
<div elif="{{defaultLevel == 1000}}" class="sub_container">
<text id="1" class="single_char" onclick="switchLevel('1')">1</text>
<text id="10" class="single_char" onclick="switchLevel('10')">2</text>
<text id="100" class="selected_single_char" onclick="switchLevel('100')">3</text>
</div>
<div class="sub_container" else>
<text id="1" class="selected_single_char" onclick="switchLevel('1')">1</text>
<text id="10" class="single_char" onclick="switchLevel('10')">2</text>
<text id="100" class="single_char" onclick="switchLevel('100')">3</text>
</div>
</div>
</div>
index.css
CSS:
.container {
background-color: #FFA7A9;
display: flex;
justify-content: center;
align-items: center;
left: 0px;
top: 0px;
width: 454px;
height: 554px;
flex-direction: column;
}
.sub_container {
background-color: transparent;
display: flex;
justify-content: center;
align-items: center;
left: 0px;
top: 0px;
width: 300px;
height: 45px;
flex-direction: row;
}
.title {
text-align: center;
width: 200px;
height: 50px;
}
.game_title {
padding: 5;
text-align: center;
width: 400px;
height: 50px;
}
.total {
color: chocolate;
text-align: center;
width: 500px;
height: 40px;
}
.settings {
margin-top: 5px;
margin-bottom: 5px;
text-align: center;
width: 300px;
height: 40px;
color:white;
}
.text_button {
background-color: blueviolet;
margin-top: 10px;
margin-bottom: 10px;
text-align: center;
width: 150px;
height: 40px;
border-radius: 10;
}
.small_title {
align-items: flex-end;
margin: 5;
background-color: coral;
text-align: center;
width: 70px;
height: 40px;
border-radius: 10;
}
.selected_single_char {
align-items: flex-end;
margin: 5;
background-color: #2E24FF;
text-align: center;
width: 40px;
height: 40px;
border-radius: 50;
}
.single_char {
align-items: flex-end;
margin: 5;
background-color: #FF2A75;
text-align: center;
width: 40px;
height: 40px;
border-radius: 50;
}
.progress_horizontal{
color: #C70009;
width: 300px;
height: 10px;
}
.below_half{
margin-top: 10px;
background-color: #AAE1E2;
display: flex;
justify-content: center;
align-items: center;
left: 0px;
top: 0px;
width: 500px;
height: 130px;
flex-direction: column;
}
.circle {
width: 50px;
height: 50px;
border-radius: 50;
color: #fff;
text-align: center;
background-color: #AAE1E2;
}
Read full article..
Conclusion
In this article, we have learned the UI components such as Progress Bar, Div, Conditional Text, Timer and Screen on API
Reference
1. Harmony Official document: https://developer.harmonyos.com/en/docs/documentation/doc-guides/harmonyos-overview-0000000000011903
2. DevEco Studio User guide: https://developer.harmonyos.com/en/docs/documentation/doc-guides/tools_overview-0000001053582387
3. JS API Reference: https://developer.harmonyos.com/en/...-references/js-apis-overview-0000001056361791
Related
Hi,
i m using relative layout for displaying webview. when left marging is zero then it is working but marging set to 150 that time it is not working properly. this error occur in android jelly bean and kindle fire 2nd generation.
Following is the code:
package com.example.relativelayoutexample;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
RelativeLayout parent,child;
RelativeLayout.LayoutParams child_params,WebView_params;
WebView WB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
parent=new RelativeLayout(this);
RelativeLayout.LayoutParams helpPopupLayout_params=new RelativeLayout
.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
parent.setLayoutParams(helpPopupLayout_params);
parent.setPadding(0, 0, 0, 0);
parent.setBackgroundColor(Color.TRANSPARENT);
parent.setBackgroundColor(Color.RED);
child=new RelativeLayout(this);
child_params=new RelativeLayout.LayoutParams(700,600);
int leftMargin=150,topMargin=0,rightMargin=0;
child_params.leftMargin = leftMargin;
child_params.topMargin = topMargin;
child_params.rightMargin = rightMargin;
child.setLayoutParams(child_params);
parent.addView(child,child_params);
WebView_params=new RelativeLayout
.LayoutParams(850,600);
WebView_params.setMargins(0, 0, 0, 0);
WB = new WebView(this);
WebSettings webSettings = WB.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
webSettings.setUseWideViewPort(true);
WB.loadUrl("http://192.168.100.11:8017/test/rajeshTest/htmls/test.html");//from server
child.addView(WB,WebView_params);
addContentView(parent, helpPopupLayout_params);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
HTML File
test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="viewport" content="user-scalable=yes,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
<title>Untitled Document</title>
<body>
<video id="bubbles" width="400" height="300" controls="controls" src="http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/0640_vod.m3u8">
<!--<iframe src="http://www.w3schools.com/cssref/pr_class_visibility.asp" width="100%" height="100%"></iframe>-->
</video>
</body>
</html>
leftmargin=0
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
leftmargin=150
Thank you,
Rajesh patil
+919766540216
Hi guys I'm having problem with the jfeistein sliding menu. I have correctly imported it but i have only a problem. When I click on the "test" item it opens the related fragment but it doesn't autoclose the menu as it should.
MAINACTIVITY
Code:
public class MainActivity extends Activity {
private SlidingMenu slidingMenu ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
slidingMenu = new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
slidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);
slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
slidingMenu.setFadeDegree(0.35f);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
slidingMenu.setMenu(R.layout.slidingmenu);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public void onBackPressed() {
if ( slidingMenu.isMenuShowing()) {
slidingMenu.toggle();
}
else {
super.onBackPressed();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
slidingMenu.toggle();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.slidingMenu.toggle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
SLIDING MENU FRAGMENT
Code:
public class SlidingMenuFragment extends Fragment implements ExpandableListView.OnChildClickListener {
private SlidingMenu slidingMenu ;
private ExpandableListView sectionListView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
List<Section> sectionList = createMenu();
View view = inflater.inflate(R.layout.slidingmenu_fragment, container, false);
this.sectionListView = (ExpandableListView) view.findViewById(R.id.slidingmenu_view);
this.sectionListView.setGroupIndicator(null);
SectionListAdapter sectionListAdapter = new SectionListAdapter(this.getActivity(), sectionList);
this.sectionListView.setAdapter(sectionListAdapter);
this.sectionListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true;
}
});
this.sectionListView.setOnChildClickListener(this);
int count = sectionListAdapter.getGroupCount();
for (int position = 0; position < count; position++) {
this.sectionListView.expandGroup(position);
}
return view;
}
private List<Section> createMenu() {
List<Section> sectionList = new ArrayList<Section>();
Section oDemoSection = new Section("Demos");
oDemoSection.addSectionItem(101,"Test", "lol");
oDemoSection.addSectionItem(102, "Airport (AsyncTask)", "lol");
Section oGeneralSection = new Section("General");
oGeneralSection.addSectionItem(201, "Settings", "lol");
oGeneralSection.addSectionItem(202, "Rate this app", "lol");
oGeneralSection.addSectionItem(203, "Eula", "lol");
oGeneralSection.addSectionItem(204, "Quit", "lol");
sectionList.add(oDemoSection);
sectionList.add(oGeneralSection);
return sectionList;
}
public void toggle() {
slidingMenu.toggle();
}
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Fragment fragment=null;
switch ((int)id) {
case 101:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, new Net()).commit();
break;
case 102:
//TODO
break;
case 201:
//TODO
break;
case 202:
//TODO
break;
case 203:
//TODO
break;
case 204:
//TODO
break;
}
return false;
}}
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
1. Map Creation
This section describes how to create a simple web-based map using the HUAWEI Map Kit API for JavaScript.
1. Create an HTML file and declare the following content in the file header so that the file is supported in most browsers:
Code:
<!DOCTYPE html>
2. Add the style tag pair in the head tag pair to set the map container dimensions.
Code:
<head>
<meta charset="UTF-8">
<style>
#map {
height: 800px;
width: 80%;
margin: 0 auto;
}
</style>
</head>
3. Provide the HUAWEI Map Kit API file. The key must be transcoded using the URL.
Code:
<script src="https://mapapi.cloud.huawei.com/mapjs/v1/api/js?callback=initMap&key=API KEY"></script>
4. Create map container elements in the body.
Code:
<body>
<div id="map"></div>
</body>
Create a script for initializing the map. The following sample code is to create a map with Paris as the center and a zoom level of 8:
Code:
<script>
function initMap() {
var mapOptions = {};
mapOptions.center = {lat: 48.856613, lng: 2.352222};
mapOptions.zoom = 8;
mapOptions.language='ENG';
var map = new HWMapJsSDK.HWMap(document.getElementById('map'), mapOptions);
}
</script>
5. View the created map.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
2. Map Attribute
You can modify the attributes of a map, such as the map center, zoom coefficient, and map heading. The following sample code is to modify the map center:
Code:
<tr>
<td>Latitude:</td>
<td><input id="latInput" type="text" value="48.95"/></td>
</tr>
<tr>
<td>Longitude:</td>
<td><input id="lngInput" type="text" value="2.35"/></td>
</tr>
Code:
var lat = Number(document.getElementById("latInput").value);
var lng = Number(document.getElementById("lngInput").value);
map.setCenter({lat: lat, lng: lng});
The following table describes the map attributes that can be customized.
Symptom
When I used the HTML attribute background: linear-gradient(angle, color-stop1, color-stop2) to add the gradient effect to an image, the gradient color does not take effect, but it can apply to the text element for a quick app.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
The code where the exception occurs is as follows:
Code:
<template>
<!-- Only one root element is allowed in template. -->
<div class="container">
<image src="/Common/img/compress.png"
style="width: 100%; height: 300px;background: repeating-linear-gradient( rgba(255, 0, 0, .5),rgba(0, 0, 255, .5));">
</image>
<text style="background: repeating-linear-gradient( rgba(255, 0, 0, .5),rgba(0, 0, 255, .5));">nice scene, beautiful </text>
</div>
</template>
Cause Analysis
The image is opaque. Therefore, even if the gradient background is set, it cannot be seen because it is covered by the image above.
Solution
You can set the stack element as a parent of the image element, set the div element to cover the image element, and set the gradient background on the div element. (You do not need to set the gradient background on the image element.)
The code (highlighted in red) for implementation is as follows:
Code:
<template>
<!-- Only one root element is allowed in template. -->
<div class="container">
<stack>
<image src="/Common/img/compress.png" style="width: 100%; height: 300px"></image>
<div style="width: 100%; height: 300px;background: repeating-linear-gradient( rgba(255, 0, 0, .5),rgba(0, 0, 255, .5));"></div>
</stack>
<text style="background: repeating-linear-gradient( rgba(255, 0, 0, .5),rgba(0, 0, 255, .5));"> nice scene, beautiful </text>
</div>
</template>
Final Effect
For more information, please refer to:
Gradient style of the quick app:
https://developer.huawei.com/consumer/en/doc/development/quickApp-References/quickapp-common-settings#h1-1575449409015
Quick app materials:
https://developer.huawei.com/consumer/en/doc/development/quickApp-Guides/quickapp-introduction
Interesting and could be useful.
Rebis said:
Interesting and could be useful.
Click to expand...
Click to collapse
Thanks for your liking.
SymptomAfter layout-type:stagger is set for the list component, the width of the list-item component cannot be changed, and the configured width does not take effect. The width of each list-item component is the same.
The code where the exception occurs is as follows:
HTML:
<template>
<div style="padding-left:30px;padding-right: 30px;">
<list style="flex-direction:row-reverse;width: 690px;">
<list-item type="zxc">
<list style="width: 690px;layout-type:stagger;display: flex;">
<list-item type="qqq" style="width: 100px;height: 300px;margin-bottom:30px;background-color: red">
<text class="font-big">Test</text>
</list-item>
<block for="{{listData}}">
<list-item type="qqq" style="width: 300px;height: 400px;margin-bottom:30px;background-color: green">
<text class="font-big">{{$item}}--{{$idx}}</text>
</list-item>
</block>
</list>
</list-item>
</list>
</div>
</template>
<style>
</style>
<script>
module.exports = {
data: {
listData:['AB','A','C','D','E']
},
onInit() {
this.$page.setTitleBar({ text: '' })
this.accept = this.body;
console.debug("message debug");
console.log("message zhengchang");
},
}
</script>
The current running effect is as follows.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Cause AnalysisWhen layout-type of the list component is set to stagger, only the height of each list-item can be customized. The width depends on the width set for the list component and the value of columns. The width of each column is equal to the total width of the list component divided by the columns.
SolutionDelete layout-type:stagger. Customize the width of list-item based on the default grid layout.
HTML:
<template>
<div style="padding-left:30px;padding-right: 30px;">
<list style="flex-direction:row-reverse;width: 690px;">
<list-item type="zxc">
<list style="width: 690px;display: flex;">
<list-item type="qqq" style="width: 100px;height: 300px;margin-bottom:30px;background-color: red">
<text class="font-big">Test</text>
</list-item>
<block for="{{listData}}">
<list-item type="qqq" style="width: 300px;height: 400px;margin-bottom:30px;background-color: green">
<text class="font-big">{{$item}}--{{$idx}}</text>
</list-item>
</block>
</list>
</list-item>
</list>
</div>
</template>
<style>
</style>
<script>
module.exports = {
data: {
listData:['AB','A','C','D','E']
},
onInit() {
this.$page.setTitleBar({ text: '' })
this.accept = this.body;
console.debug("message debug");
console.log("message zhengchang");
},
}
</script>
The modified running effect is as follows.
Checkout in forum