Computer >> Máy Tính >  >> Lập trình >> Android

Làm thế nào để đảo ngược chuỗi trong Android?


Ví dụ này trình bày Cách thực hiện đảo ngược chuỗi trong Android

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:orientation = "vertical">
   <EditText
      android:id = "@+id/name"
      android:layout_width = "match_parent"
      android:hint = "Enter Name"
      android:layout_height = "wrap_content" />
   <LinearLayout
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content">
      <Button
         android:id = "@+id/save"
         android:text = "Save"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" />
   </LinearLayout>
   <TextView
      android:id = "@+id/textview"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent" />
</LinearLayout>

Trong đoạn mã trên, chúng tôi đã lấy tên và khi người dùng nhấp vào nút, nó sẽ in ngược lại giá trị văn bản chỉnh sửa.

Bước 3 - Thêm mã sau vào src / MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
   EditText name;
   HashMap<Character, Integer> charCountMap;
   TextView textview;
   @Override
   protected void onCreate(Bundle readdInstanceState) {
      super.onCreate(readdInstanceState);
      setContentView(R.layout.activity_main);
      name = findViewById(R.id.name);
      textview = findViewById(R.id.textview);
      charCountMap = new HashMap<>();
      findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if (!name.getText().toString().isEmpty()) {
               StringBuffer c = new StringBuffer(name.getText().toString());
               textview.setText(c.reverse());
               Toast.makeText(MainActivity.this, "Inserted", Toast.LENGTH_LONG).show();
            } else {
               name.setError("Enter NAME");
            }
         }
      });
   }
}

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm thế nào để đảo ngược chuỗi trong Android?

Trong kết quả trên, chúng tôi đang chèn tên nhưng textview hiển thị theo thứ tự revese.