“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”

👉 About the author

As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!

Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)

Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!

👉 premise

TextView: TextView, TextView, TextView, TextView, TextView, TextView, TextView, TextView, TextView

This is the Android series written by Xiao Kong, welcome to taste.

👉 Practice

Button is an important component for users to interact with each other in the program. It is also one of the basic components with high utilization rate in ordinary projects. Of course, any View can handle a click, but a Button is more appropriate in most cases.

😜 met

Let’s create a basic Button:


      
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TextActivity">
    <Button
       android:id="@+id/mtBtn"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Here's the button." />
    <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentRight="true"
       android:text="this button" />
</RelativeLayout>
Copy the code

The following information is displayed:

Well, the Chinese character display is normal, but the English code is not lowercase, why the display is uppercase.

It’s the property textAllCaps. Let’s see if it works.

<Button
    android:id="@+id/mtBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:textAllCaps="false"
    android:text="this button" />
Copy the code

😜 accident

At this time a friend asked: no ah, in addition to English your color and mine are different ah, obviously I also write the same code with you.

The default created project has a theme, of course you can customize it.

In the [Android :theme] property of the [Androidmanifest.xml] file, click [Ctrl+ left] and you will find some color definitions. And you should pay attention to the layout preview screen also has the choice of theme, careful pit.

Let’s drop the theme for a moment and move on to Button.

Since it’s a button, interaction is a must. How do you set up click events?

public class TextActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);
        Button btn = findViewById(R.id.mtBtn);
        btn.setOnClickListener(view -> {
            Toast.makeText(TextActivity.this."Click the button.", Toast.LENGTH_SHORT).show(); }); }}Copy the code

After running, click the button to see the effect.

Off-topic: In App, in addition to users’ frequent clicks without intention, sometimes the network request is slow, users will also keep clicking on it intentionally. If you do not handle it, you will repeatedly handle it, which is definitely not compliant. So here’s a solution to prevent multiple button clicks:

  • Determines the time since the last click in the click event.
  • Encapsulate a click event inside the same is to determine the click time, but the subsequent view click events all use this.
  • Writing a static method also determines the time of the click. Check whether the static time returns true or false on the first line of the system’s click event

👉 other

📢 author: Kom and Kom in Kom

📢 reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)

📢 welcome to like 👍 collect 🌟 message 📝