Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

จะใช้ CompareTo () ใน textview ของ Android ได้อย่างไร?


ตัวอย่างนี้สาธิตเกี่ยวกับวิธีใช้ CompareTo () ในมุมมองข้อความของ Android

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน 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:hint="Enter name"
      android:layout_height="wrap_content" />
   <Button
      android:id="@+id/click"
      android:text="Click"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
   <TextView
      android:id="@+id/textview"
      android:layout_width="wrap_content"
      android:textSize="25sp"
      android:layout_height="wrap_content" />
</LinearLayout>

ในโค้ดด้านบนนี้ เราได้ตั้งชื่อเป็น Edit text เมื่อผู้ใช้คลิกที่ปุ่ม มันจะดึงข้อมูลและเปรียบเทียบกับสตริง sairam

ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน src/MainActivity.java

package com.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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) {
            if (!name.getText().toString().isEmpty()) {
               if (name.getText().toString().length() >=0) {
                  text.setText(String.valueOf(name.getText().toString().compareTo("sairam")));
               }
            } else {
               name.setError("Plz enter name");
            }
         }
      });
   }
}

มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

จะใช้ CompareTo () ใน textview ของ Android ได้อย่างไร?

จากผลลัพธ์ข้างต้น ให้ป้อนสตริงที่ไม่เหมือนกัน และ a มีค่าน้อยกว่า s -18 เท่า ตอนนี้ป้อน sairam ใน editext และคืนค่า 0 หมายถึง true ดังที่แสดงด้านล่าง -

จะใช้ CompareTo () ใน textview ของ Android ได้อย่างไร?