Android SDK Getting Started Guide||Chapter 6 User Interaction

Chapter 6 User Interaction

In this tutorial, we will set up the Button element we added previously to detect and respond to user clicks. To achieve this, we need to dabble a little bit in Java programming in the main Activity class of our application. If you don’t have much experience in Java development, there is no need to worry. Just follow the steps to complete the learning. In the next article in this series, we will delve into Java syntax to ensure that you understand the programming language knowledge necessary for initial Android development tasks.

You can implement user interaction in Android in many different ways. We will learn two of the most typical solutions to implement application button sensing of user clicks - both solutions will use a little XML code and Java implementation processes. Android contains several different interactive UI elements, which are sufficient to sense various input operations from users. Input actions have to be handled to match UI items, but the overall process is still largely the same. We'll start exploring user interaction on the Android platform with a button, since buttons are undoubtedly the simplest and most commonly used interface element.

1. User interaction basics

Before going into details, I want to first explain a few UI concepts for those who are new to application development. In order to achieve application interaction, we need to use specific elements to detect user interaction. Friends who have read the previous article must remember that View exists in Android, and in today's example it refers to Button. To achieve interaction, we first need to "listen" to the user's actions. Although Android mainly runs on mobile devices equipped with touch screens, you can still use programming languages ​​​​to handle interactive development on computers. For example, in the part where "click" is mentioned later, we refer to clicking with a mouse or touching/tapping a corresponding position with a finger.

There are many ways users interact with applications. They can tap, swipe, and "long press" on items. When these operational activities occur, we call it an "event." Therefore, we need to set up the application to listen for specific events on specific UI items. In today's example, we need to monitor the click (or tap/touch) operation on the Button.

We need to listen for and respond to such user events. To do this, we will add code to the Java Activity class to listen for and respond to button clicks. This part of the code will start executing whenever there is a click event on the button. Although other types of user interaction involve different method codes and various event types, the basic process is the same.

2. Identify UI elements

first step

In order to indicate which View the user interaction is directed to, we need to identify each interactive View in the application. In the examples listed in the article, we only discuss one View - but when you actually develop applications in the future, you may use many different types of interactive Views. In order for them to work well with each other, we need to set a unique ID attribute for each View for identification and apply it to the entire application. First open our main layout file in Eclipse and switch to the XML editing tab. Next find the code we added for the Button element and assign it an ID using the following syntax:

android:id="@+id/myButton"

We need to assign an ID attribute to each element used in the Android layout to help us successfully identify each View element. Please note the "@+id" syntax in the above code. This prompts the Android tools to create a new ID in the project resource "R.java" file and assign it a text string that is unique within the application, which is "myButton". We will use this name to refer to the Button View throughout the rest of the XML layout code and even other XML and Java files in the application. Then save the current layout file.

second step

Open the main Activity file in the application. We'll be adding a little bit of Java code to it, but don't worry about your awesome Java skills, as long as you understand the general flow of handling user interaction. If your friends have never been exposed to Java before, please continue to pay attention to our next tutorial. When you look back, you will find that the current content is actually very simple. We need to create a variable in the Activity class to reference the Button View. At the beginning of the class declaration, after the initial content:

public class MainActivity extends Activity {
   
   

Add variable declaration:

private Button theButton;

Our declaration contains visual attributes (more on that next time), variable types, and variable names. Eclipse may underline the "Button" text and say "Button cannot be parsed as a type". Since we are using the Button type provided by the Android platform, it must be imported into the class file. Hover the mouse over the "Button" text and Eclipse will show us a list of suggestions. In it select "Import 'Button' (android.widget)". In this way, a list of import declarations that can be freely expanded and collapsed will appear at the top of the class file.

6.1eclipse_import_prompt

third step

Now we can retrieve a reference to the Button View in the layout and store that reference in the variable we created. In my Activity's onCreate method, the following line of code immediately follows the layout setup:

setContentView(R.layout.activity_main); 

Enter a new line of code as follows to get the Button back:

    theButton = (Button)findViewById(); 

Type "R." in the brackets of "findViewById()" - Eclipse will provide us with a list of resource type hints. Select "id" there.

The above code will specify the name of the method to be executed after the button is clicked. Corresponding methods should be added to the Activity class displayed in the layout. This way, we don't have to add a lot of code to the Activity class, including creating a Button variable, saving a View reference in it, implementing an OnClickListener, or setting up a dedicated click listener class for the button. In this example, we can replace adding the onClick method to the class by adding the following code (using the same code for consistent operation):

public void buttonClicked(View v){
    Button theButton = (Button)v;
    theButton.setText("Ouch");
}

Although this approach may seem simpler, the process of using Java to point references to layout elements is worthy of careful attention - you will use it frequently in the future application development process. In addition, if your layout contains multiple clickable items, you may prefer to handle all click events in the same method - in this case, the solution mentioned earlier in the article will be more ideal.

In addition to the two solutions mentioned in the article, we can also implement click processing tasks on the View through various other ways, but other methods are more complicated and not suitable for use as a novice teaching.

second step

In this tutorial, we learned how to handle button click events in the Android system in the most basic way. The platform also provides processing capabilities for a series of other user events for different View types, including long press, key press and touch, etc. Interested friends can refer to the Android Developer Guide to learn about various event processing tasks that they may be exposed to in future project development work.

Summarize

In this part, we discussed how to follow the basic process to implement the response to the user's button click in the Android UI. The content covered today is only a drop in the bucket compared to the entire Android user interaction mechanism, but everyone should be able to understand the backbone from this general method and guide themselves in future projects to produce development results that conform to user input habits. In other tutorials in this series, we will understand the most essential main features of the Java language, so as to achieve brilliant victories one after another in learning Android development.

Guess you like

Origin blog.csdn.net/m0_69824302/article/details/132480549