【问题标题】:How set the string format in editText如何在editText中设置字符串格式
【发布时间】:2016-10-15 16:17:14
【问题描述】:

我希望字符串显示在我的 EditText 中,如下所示:

"AA-BB-11",但是我只需要放在AABB11里面,可以吗?

【问题讨论】:

  • 您拥有"AA-BB-11" 并且想要AABB11?还是反过来?

标签: android string android-edittext


【解决方案1】:

我认为你必须实现 TextWatcher。尝试类似的方法:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText test = (EditText) findViewById(R.id.editText) 
        test.addTextChangedListener(this);
    }

    @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 text) {
      if (text.length() == 2 || text.length() == 5) {
        text.append('-');
      }
    }

见:https://developer.android.com/reference/android/text/TextWatcher.html

编辑:

您可以在此处查看完整代码:https://stackoverflow.com/a/28043245/6450234

您只需将text.length() == 3 || text.length() == 7 更改为text.length() == 2 || text.length() == 5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多