Java 在Android中,在字符串的2位字符后添加空格
如果我输入5位、6位或7位字符,那么我只想在字符串的2位、3位或4位字符后添加一个空格。有人能帮我弄清楚怎么做吗Java 在Android中,在字符串的2位字符后添加空格,java,android,android-layout,Java,Android,Android Layout,如果我输入5位、6位或7位字符,那么我只想在字符串的2位、3位或4位字符后添加一个空格。有人能帮我弄清楚怎么做吗 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.addTextChangedListen
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, " ");
}
}
});
}
这也许不是最好的答案,但它是有效的。我使用了
textview
和edittext
。接下来,我将edittext
字体颜色设置为透明。之后,我转到java文件,在edittext上,我设置了一个文本监视程序。因此,每当用户键入内容时,您的textwatcher就会获得实际输入。因此,根据这一点,您可以修改字符串并在textview
上设置字符串。这就产生了想要的幻觉。
以下是部分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>
[可能重复]到目前为止您尝试了什么?我使用了文本监视程序:if(!s.toString().包含(“”&&s.length()>5)){s.insert(2,“”;}如果(!s.toString().包含(“”&(s.length()>6)){s.insert(3,“”;}如果(!s.toString().包含(“”&&s.length()>7)){s.insert(4,“”;}请将此添加到您的问题中,以及您如何使用您的TextWatcher以及您现在看到的行为我添加,请检查它是否工作正常非常感谢您的逻辑我不认为这是我心目中的逻辑。
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);
}
}