简体   繁体   English

在Android中的字符串的2位字符后添加空格

[英]Add spaces after 2 digits characters of a string in Android

If i enter 5 digits or 6 digits or 7 digits character then I just want to add a space after 2 digit or 3 digits or 4 digits character of a string.如果我输入 5 位或 6 位或 7 位字符,那么我只想在字符串的 2 位或 3 位或 4 位字符后添加一个空格。 Can anyone help me figure out how to do this?谁能帮我弄清楚如何做到这一点?

E.g. given "M11AE", I need "M1 1AE" as the result.
E.g. given "B338TH", I need "B33 8TH" as the result.
E.g. given "DN551PT", I need "DN55 1PT" as the result.


 postCodeEt.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, 
 int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int 
   count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

           if ((!s.toString().contains(" ") && s.length() > 5)) {
                s.insert(2, " ");
            }else if (!s.toString().contains(" ") && (s.length()>6)) {
                s.insert(3, " ");
            }else if (!s.toString().contains(" ") && (s.length()>7)) {
                s.insert(4, " ");
            }

        }
    });
}

this may not be the best answer, but it works.这可能不是最好的答案,但它有效。 I took a textview and edittext .我拿了一个textviewedittext Next, I set the edittext font color to transparent.接下来,我将edittext字体颜色设置为透明。 After that, I went to the java file and on the edittext, I set a text watcher.之后,我转到java文件并在edittext上设置了一个文本观察器。 So whenever the user types something, your textwatcher gets the actual input.因此,每当用户键入内容时,您的 textwatcher 都会获取实际输入。 So depending on that, you modify the string and set the string on the textview .因此,根据这一点,您可以修改字符串并在textview上设置字符串。 So this gives the desired illusion.所以这给出了想要的错觉。 Here is the partial xml file:这是部分xml文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:background="#FFFF00"
        android:id="@+id/myTextView"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:text=""
        android:gravity="center_vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:id="@+id/myEditText"
        android:layout_width="200dp"
        android:layout_height="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textColor="@android:color/transparent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

And here is the java code:这是java代码:

package com.applications.invisible;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView myTextView;
    EditText myEditTExt;
    String actualText;
    String visibleText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        myTextView = findViewById(R.id.myTextView);
        myEditTExt = findViewById(R.id.myEditText);

        myEditTExt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                actualText = myEditTExt.getText().toString();
                visibleText = myVisibleText(actualText);
                myTextView.setText(visibleText);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    private String myVisibleText(String actualText) {
        String ret = "";
        if(actualText.length() == 5){
            ret = actualText.substring(0,2)+" "+actualText.substring(2);
        }else if(actualText.length() == 6){
            ret = actualText.substring(0,3)+" "+actualText.substring(3);
        }else if(actualText.length() >= 7){
            ret = actualText.substring(0,4)+" "+actualText.substring(4);
        }else{
            ret = actualText;
        }
        return ret;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM