首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android:如何使用相同的编辑文本作为用户名和电话号码

Android:如何使用相同的编辑文本作为用户名和电话号码
EN

Stack Overflow用户
提问于 2018-03-09 04:13:22
回答 2查看 1.1K关注 0票数 2

问题:用户应该能够通过他们的电话号码或用户名(在注册过程中设置的内容)登录。这两个值都允许用户登录。当用户输入数字作为第一个字符时,Edittext字段需要足够聪明以确定什么是entered.So,它应该自动添加国家代码电话号码前缀,并在前缀之后插入类型化字符。

这是我的布局:

代码语言:javascript
复制
 <LinearLayout
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/emaillayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:animateLayoutChanges="true"
                android:background="@drawable/login_field"
                android:padding="2dp"
                android:paddingStart="5dp"
                android:visibility="visible">

                <AutoCompleteTextView
                    android:id="@+id/email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:hint="@string/prompt_email"
                    android:inputType="textEmailAddress"
                    android:maxLines="1"
                    android:padding="2dp"
                    android:singleLine="true" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/phlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:animateLayoutChanges="true"
                android:background="@drawable/login_field"
                android:orientation="horizontal"
                android:padding="2dp"
                android:paddingStart="5dp"
                android:visibility="gone">


                <EditText
                android:id="@+id/et_ph"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:ems="10"
                android:hint="Phone number"
                android:inputType="phone" />

            </android.support.design.widget.TextInputLayout>

        </FrameLayout>



        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputLayout

            android:id="@+id/pwdlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"

            android:background="@drawable/login_field"
            android:orientation="horizontal"
            android:padding="2dp"
            android:paddingStart="5dp">

                <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:hint="@string/prompt_password"
                android:imeActionId="6"
                android:imeActionLabel="@string/action_sign_in_short"
                android:imeOptions="actionUnspecified"
                android:inputType="textPassword"
                android:maxLines="1"
                android:padding="2dp"
                android:singleLine="true" />
            </android.support.design.widget.TextInputLayout>

            <TextView
                android:id="@+id/pwdtoggle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end|center_vertical"
                android:text="SHOW"
                android:layout_marginRight="16dp"
                android:paddingTop="10dp"
                />
        </FrameLayout>

        <Button

            android:id="@+id/email_sign_in_button"
            style="?android:textAppearanceSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginTop="16dp"
            android:background="@drawable/sausage_button"
            android:text="@string/action_sign_in"
            android:textStyle="bold" />


    </LinearLayout>

这是我活动中的代码:

代码语言:javascript
复制
emailView.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(isNumberOnly(s.toString()))
            {
                ChangeToPhoneNumber(s.toString());
            }

        }
    });

    numberView.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.length()==0)
            {
               ChangeToUsername();
            }

        }
    });


}

private void ChangeToPhoneNumber(String s)
{

    emaillayout.setVisibility(View.GONE);
    phlayout.setVisibility(View.VISIBLE);
    numberView.setText("+61" + s);
    numberView.setSelection(mPhonenumberView.getText().length());
    mPhonenumberView.requestFocus();


}

private void ChangeToUsername()
{

        emaillayout.setVisibility(View.VISIBLE);
        phlayout.setVisibility(View.GONE);
        emailView.setText("");
        emailView.requestFocus();


}



public boolean isNumberOnly(String s)
{
    return s.matches("-?\\d+(.\\d+)?");
}

因此,通过这个实现,我能够实现我想要的。以下是截图:

虽然我可以实现这些结果,但我觉得一直调用文本观察者可能会带来一定的开销,因为我使用的是两个单独的视图,并添加了两个Textwatcher实例。怎样才能使这更清洁、更有效率?另外,当我按下键盘上的backspace键时,我想了解如何确保前缀不被擦除,但将视图更改为username字段?

EN

回答 2

Stack Overflow用户

发布于 2018-03-09 04:20:02

可以使用一个EditText来实现如下所示的功能

代码语言:javascript
复制
mPhoneAndEmailView.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(isNumberOnly(s.toString()) && !mPhoneAndEmailView.contains("+61")){
             mPhoneAndEmailView.setText("+61" + s);
          }//utlimately when you clear number and start typing text it will ignore if and takes alpha or if it has only numbers first time it will append +61

        }
    });
票数 0
EN

Stack Overflow用户

发布于 2018-03-09 06:27:45

用这种方式试试它对我的作用。我只使用一个EditText并以编程方式更改所有属性。

MainActivity.java

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {
TextInputLayout textInputLayout;
AppCompatEditText edtPhnUsername;

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


    textInputLayout = findViewById(R.id.textInputLayout);
    edtPhnUsername = findViewById(R.id.edtPhnUsername);


    edtPhnUsername.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) {
            new Thread(new Runnable() {
                @Override
                public void run() {

                    changeEditTextState();
                }
            }).start();


        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    });


}


private void changeEditTextState() {
    String uname = edtPhnUsername.getText().toString().trim();

    if (!TextUtils.isEmpty(uname)) {
        String desiredString = "";

        if (uname.length() >= 3) {
            desiredString = uname.substring(0, 3);
        }

        if (uname.contains("+61") && desiredString.equalsIgnoreCase("+61")) {
            changeToPhoneNumber();
        } else {
            changeToUsername();
        }
    } else {
        //default
        changeToUsername();
    }

}

private void changeToPhoneNumber() {
    textInputLayout.setHint("Phone number");
    edtPhnUsername.setInputType(InputType.TYPE_CLASS_PHONE);

    setEditTextMaxLength(10);



}

private void changeToUsername() {
    textInputLayout.setHint("Username");
    edtPhnUsername.setInputType(InputType.TYPE_CLASS_TEXT);
    setEditTextMaxLength(50);
}

public void setEditTextMaxLength(int length) {
    InputFilter[] filterArray = new InputFilter[1];
    filterArray[0] = new InputFilter.LengthFilter(length);
    edtPhnUsername.setFilters(filterArray);
}



}

activity_main.xml

代码语言:javascript
复制
 <android.support.design.widget.TextInputLayout
    android:id="@+id/textInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:orientation="horizontal"
    android:paddingStart="5dp"
    android:hint="Username"
    android:paddingLeft="5dp">


    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/edtPhnUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:maxLines="1"
        android:layout_weight="1" />

</android.support.design.widget.TextInputLayout>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49186507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档