【问题标题】:How to make edit text like this?如何制作这样的编辑文本?
【发布时间】:2016-08-29 04:20:42
【问题描述】:

当我点击编辑文本时 enter image description here 最后输入手机号码 enter image description here

如何制作这样的EditText,在android中使用国家代码进行移动输入? 如果有人知道此代码,请提前分享并感谢所有人。

【问题讨论】:

  • 你已经尝试过什么?如果您尝试过某些操作,请编辑您的问题以包含您的代码。如果没有,请先尝试自己解决问题,然后再提出关于 SO 的问题,因为我们不会为您编写代码。

标签: android input android-edittext custom-controls


【解决方案1】:

试试下面的 XML 代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Mobile Number"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+971"
        android:id="@+id/number"
        android:cursorVisible="false"
        android:textSize="20sp"/>
    <View
        android:layout_width="2dp"
        android:layout_height="40dp"
        android:background="@android:color/darker_gray"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter number"
            android:id="@+id/ed2"
            android:cursorVisible="true"
            android:layout_gravity="top"
            android:textSize="20sp">
    </EditText>
</LinearLayout>

【讨论】:

    【解决方案2】:

    在此图像中使用了 2 个编辑框,一个用于获取前 3 个数字(国家代码),另一个用于获取其他数字

    【讨论】:

    • 如何做中心线和顶部文字“手机号码”和下划线
    • @EbinFrancis 用于垂直短线取景,宽度为 1dp,高度为您所需要的。对于水平线宽,您需要高度 1dp
    【解决方案3】:

    更新答案:

    Youtube Demo

    public class MainActivity extends AppCompatActivity {
    
        Context context;
        String Crcode = "+ 971" + "|";
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            context = this;
    
            TextView code = (TextView) findViewById(R.id.textView2);
            code.setText(Crcode);
    
            EditText number = (EditText) findViewById(R.id.editText);
            number.setText(" 2 877 2877");
            number.setSelection(number.getText().length());
        }
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/base"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@color/white"
        android:orientation="horizontal"
        android:weightSum="10">
    
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:text="Mobile Number" />
    
    
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView"
            android:orientation="horizontal">
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="+971 | "
                android:textColor="#111111"
                android:textSize="20dp" />
    
            <EditText
                android:id="@+id/editText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@null"
                android:ems="10"
                android:inputType="phone"
                android:text=" 2 877 2877"
                android:textColor="#111111"
                android:textSize="20dp" />
    
    
        </LinearLayout>
    
        <View
            android:id="@+id/View2"
            android:layout_width="wrap_content"
            android:layout_height="1dp"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/linearLayout"
            android:layout_marginTop="5dp"
            android:layout_toEndOf="@+id/textView"
            android:background="#111111" />
    
    
    </RelativeLayout>
    

    【讨论】:

    • 不像这个人,这是我需要的类型,当我输入数字时,编辑文本视图会发生变化
    • 添加editext观察器并在其中添加您的前缀代码。
    【解决方案4】:

    添加国家代码 3 位数的示例,例如 +91 或 +61

      boolean isDeletePressed;
    
       edittext.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                isDeletePressed = (i == KeyEvent.KEYCODE_DEL);
                return false;
            }
        });
    
    
    
    
     edittext.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) {
    
            }
    
            @Override
            public void afterTextChanged(Editable editable) {
    
    
    
                if (editable.toString().length() == 3) {
                    if (!isDeletePressed) {
                        edittext.setText(edittext.getText().toString() + "|");
                        edittext.setSelection(edittext.getText().toString().length());
                    }
                } else if (editable.toString().length() == 4) {
                    if (!(editable.toString().charAt(3) == '|')) {
                        edittext.setText(edittext.getText().toString().substring(0, 3) + "|" + edittext.getText().toString().substring(3));
                        edittext.setSelection(edittext.getText().toString().length());
                    }
                }
    
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多