- This is the fourth day of my participation in the August More Text Challenge.
1. Three actions for sliding events
- The interface name:
TouchEventListener
- The sliding event is divided into three actions: hold down, move and lift.
PRIMARY_POINT_DOWN: Press not loose. POINT_MOVE: move. PRIMARY_POINT_UP: lift.Copy the code
- Method return value:
trueContinue to perform the following operations.falseIndicates that subsequent operations will not continue.Copy the code
- Involves the following three actions, according to the user press position and the panasonic position, you can identify the user is up, down, left, or right slide.
- For example, you can tell if the user is swiping right.
- For example, you can tell if the user is sliding down.
2. Implementation case: Press, move or release to modify the content of the text
- Because you have to slide across the screen, give the outermost layout
DirectionalLayout
Set the slide event, add aid
- Press, move, or lift to modify the content of the text
- New project: ListenerApplication4
- Code implementation
ability_main
- Take the default generated Text Text content, based on this
DirectionalLayout
The layout andText
Components separately addid
<DirectionalLayout
ohos:id="$+id:dl"
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<Text
ohos:id="$+id:text1"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="$string:mainability_HelloWorld"
ohos:text_size="40vp"
/>
</DirectionalLayout>
Copy the code
MainAbilitySlice
- It is written using the current class as the implementation of the class interface
package com.xdr630.listenerapplication.slice;
import com.xdr630.listenerapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.multimodalinput.event.TouchEvent;
public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener {
Text text1 = null;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//1. Find the entire layout object first
DirectionalLayout dl = (DirectionalLayout) findComponentById(ResourceTable.Id_dl);
text1 = (Text) findComponentById(ResourceTable.Id_text1);
//2. Add a sliding event to the layout
// The onTouchEvent method of this class is called as we slide through the layout
// The onTouchEvent method in this class is constantly called by the code during the move/release process
dl.setTouchEventListener(this);
}
@Override
public void onActive(a) {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
@Override
public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
// Parameter 1: Component represents a sliding component (layout is also a component, so you can also use component to represent layout objects)
// This represents the DirectionalLayout layout object, which covers the entire screen
// Parameter 2: touchEvent represents the action object (press, slide, lift)
// Get the current finger to operate the screen (press, slide, lift)
int action = touchEvent.getAction();
// 1: press down
// 2: release operation
// 3. Indicates a slide/move operation
if (action == TouchEvent.PRIMARY_POINT_DOWN){
// Just write the code that needs to run when pressed
text1.setText("Press");
}else if (action == TouchEvent.POINT_MOVE){
// Move or slide
text1.setText("Mobile");
}else if (action == TouchEvent.PRIMARY_POINT_UP){
// Release or lift
text1.setText("Let go");
}
return true; }}Copy the code
- Run:
- Press:
- Mobile:
- Loosen the:
3. Press, slide, or release
- You can see
One, two, three
The numbers represent separatelyPRIMARY_POINT_DOWN (press), PRIMARY_POINT_UP (release), POINT_MOVE (move)
.Therefore, the parameters of the above code can also be directly replaced by numbers, but for more intuitive expression, it is recommended to use parameters, at a glance. - For example, use numbers
if (action == 1) {// Just write the code that needs to run when pressed
text1.setText("Press");
}else if (action == 3) {// Move or slide
text1.setText("Mobile");
}else if (action == 2) {// Release or lift
text1.setText("Let go");
}
Copy the code
4. Verify the press, move, release process, and the code will repeatedly call the onTouchEvent method in this class
- Based on the above code, define a member variable counter
int count = 0
- The onTouchEvent method is called once, so it’s added once
- the
count
After each operation
- When pressed, that’s the first call,
count
Should be1
- When moving with the mouse constantly moving, it will constantly call
onTouchEvent
Method,count
Will be increasing
- It’s also called once when it’s released,
count
Add to the previous values1
- Therefore, after verification:
The onTouchEvent method in this class is repeatedly called by the code as it presses, moves, and releases.