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

Cách sử dụng lastIndexOf() trong TextView Android

Giới thiệu

Ví dụ này minh họa cách sử dụng phương thức lastIndexOf() để tìm vị trí xuất hiện cuối cùng của một chuỗi con trong TextView 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 cần thiết để 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"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Nhập chuỗi" />

    <Button
        android:id="@+id/click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tìm vị trí cuối 'sai'" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp" />

</LinearLayout>

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

package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    EditText name;
    Button button;
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = findViewById(R.id.name);
        button = findViewById(R.id.click);
        text = findViewById(R.id.textview);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = name.getText().toString().trim();
                if (!input.isEmpty()) {
                    // Tìm index cuối cùng của chuỗi "sai"
                    int lastIndex = input.lastIndexOf("sai");
                    text.setText("Vị trí cuối cùng của 'sai': " + lastIndex);
                } else {
                    name.setError("Vui lòng nhập chuỗi");
                }
            }
        });
    }
}

Chạy ứng dụng

Kết nối thiết bị Android thật với máy tính. Trong Android Studio, mở file activity và nhấn biểu tượng Run trên thanh công cụ. Chọn thiết bị của bạn và quan sát màn hình thiết bị.

Kết quả

Nhập chuỗi "Krishna sai sai" → Kết quả: "Vị trí cuối cùng của 'sai': 12"

Giải thích: Chuỗi "Krishna sai sai" có indices: K(0) r(1) i(2) s(3) h(4) n(5) a(6) [space](7) s(8) a(9) i(10) [space](11) s(12) a(13) i(14). Chuỗi "sai" xuất hiện lần cuối bắt đầu tại index 12.

Lưu ý: lastIndexOf(String str) trả về index bắt đầu của lần xuất hiện cuối cùng. Trả về -1 nếu không tìm thấy. Có overload lastIndexOf(String str, int fromIndex) để tìm ngược từ vị trí cụ thể.