当我点击edittext时 enter image description here 最后输入手机号码 enter image description here
如何制作这样的EditText,手机在Android中输入国家代码? 如果有人知道这个代码,请提前分享并感谢所有人。
答案 0 :(得分: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>
答案 1 :(得分:0)
在此图像中使用2个编辑框,一个用于获取前3个数字(国家代码),另一个用于获取其他数字
答案 2 :(得分:0)
添加国家代码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());
}
}
}
});
答案 3 :(得分:0)
更新答案:
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>