我有一个关于TextWatcher.I的问题。我有一个编辑框(输入类型是电话)
我想做的是:当应用程序打开时,在edittext中会显示如下格式。
http://hizliresim.com/vnDXp4
当用户输入数字时,下划线会自动删除。
当我运行我的项目时,我可以在图片中显示格式,这要归功于“之前”变量,但是如何为这个process.Do使用onTextChanged方法你有什么想法吗?
我的代码:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import java.lang.reflect.Array;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
EditText edittext1;
String before ="_ _ _ - _ _ _ _ _ _ _";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext1=(EditText)findViewById(R.id.editText1);
edittext1.setText(before);
edittext1.addTextChangedListener(textWatcher);
}
TextWatcher textWatcher=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)
{
//char single=0;
String character=edittext1.getText().toString();
//System.out.println(character);
delete();
}
@Override
public void afterTextChanged(Editable s)
{
}
};
public void delete()
{
for(int i=0; i<=before.length(); i=i +2)
{
before.substring(i);
}
}}
我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<EditText
android:layout_width="350sp"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:layout_marginTop="150dp"
android:layout_centerHorizontal="true"
android:inputType="phone"
android:textSize="40sp"
android:maxLength="24"
/>
发布于 2016-01-29 18:42:30
您可以将提示添加到文本视图中,这样当您键入提示时,提示将消失。如下所示:将其添加到textview android:hint="phone_number“
https://stackoverflow.com/questions/35081974
复制相似问题