Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách thay đổi vị trí Toast trong Android

Giới thiệu về Toast trong Android

Toast là một lớp con của java.lang.Object được sử dụng để hiển thị thông báo ngắn trong một khoảng thời gian ngắn trước khi tự động biến mất.

Ví dụ: Thay đổi vị trí Toast

Ví dụ này minh họa cách tùy chỉnh vị trí hiển thị của Toast trong ứng dụng Android.

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền đầy đủ thông tin để tạo dự án mới.

Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center"
    android:background="#33FFFF00"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text"
        android:textSize="18sp"
        android:textAlignment="center"
        android:text="Nhấn để hiển thị Toast ở trên cùng"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Trong mã trên, chúng ta sử dụng một TextView. Khi người dùng nhấn vào TextView, Toast sẽ hiển thị ở phía trên màn hình.

Bước 3: Viết logic (src/MainActivity.java)

package com.example.andy.myapplication;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    int view = R.layout.activity_main;
    TextView text;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(view);
        text = findViewById(R.id.text);

        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(MainActivity.this, "Toast mẫu", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP, 0, 250);
                toast.show();
            }
        });
    }
}

Giải thích mã quan trọng

Đoạn mã sau đặt vị trí Toast:

Toast toast = Toast.makeText(MainActivity.this, "Toast mẫu", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 0, 250);
toast.show();
  • Gravity.TOP: Căn chỉnh Toast ở phía trên
  • 0: Độ lệch trục X (ngang)
  • 250: Độ lệch trục Y (dọc) tính bằng pixel

Chạy ứng dụng

Kết nối thiết bị Android thực với máy tính. Trong Android Studio, mở file Activity và nhấn biểu tượng Run. Chọn thiết bị của bạn và quan sát kết quả:

  • Màn hình mặc định hiển thị TextView
  • Khi nhấn vào TextView, Toast xuất hiện ở vị trí trên cùng (cách mép trên 250px)