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

Làm cách nào để loại bỏ khoảng trắng cho textview trong Android?

Ví dụ này trình bày Cách loại bỏ khoảng trắng cho textview 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ẽ xóa giá trị editext và nối các giá trị vào textview.

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.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
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()) {
               String strArray = name.getText().toString().toString().replaceAll("\\s+", "");;
               textview.setText(strArray);
               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 cách nào để loại bỏ khoảng trắng cho textview trong Android?

Trong kết quả trên, chúng tôi đang chèn tên nhưng trong chế độ xem văn bản không có dấu cách trắng.