ソースを参照

PhoneAreaCodeEditView.java 添加

niusongtao@dl-cg.com 5 年 前
コミット
12ce606de7

+ 85 - 0
base_library/src/main/java/com/mgtech/base_library/custom/PhoneAreaCodeEditView.java

@@ -0,0 +1,85 @@
+package com.mgtech.base_library.custom;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.graphics.drawable.Drawable;
+import android.util.AttributeSet;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.EditText;
+import android.widget.TextView;
+
+import androidx.constraintlayout.widget.ConstraintLayout;
+
+import com.mgtech.base_library.R;
+
+/**
+ * @ProjectName: BaseLibrary
+ * @Package: com.tennor.base_library.custom
+ * @ClassName: PhoneAreaCodeEditView
+ * @Description: 带手机区号edittext
+ * @Author: 牛松涛
+ * @CreateDate: 2020/4/20 14:52
+ * @UpdateUser: 更新者:
+ * @UpdateDate: 2020/4/20 14:52
+ * @UpdateRemark: 更新说明:
+ * @Version: 1.0
+ */
+public class PhoneAreaCodeEditView extends ConstraintLayout {
+    private TextView areaCodeText;
+    private EditText phoneEdit;
+    private View lineView;
+
+    public PhoneAreaCodeEditView(Context context) {
+        super(context);
+    }
+
+    public PhoneAreaCodeEditView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        initView(context);
+        setView(context, attrs);
+    }
+
+    public PhoneAreaCodeEditView(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+    }
+
+    private void initView(Context context){
+        LayoutInflater.from(context).inflate(R.layout.layout_phone_area_code_edit,this);
+        areaCodeText = findViewById(R.id.area_code);
+        phoneEdit = findViewById(R.id.area_phone_num);
+        lineView = findViewById(R.id.area_line);
+    }
+
+    private void setView(Context context, AttributeSet attr){
+        TypedArray typedArray = context.obtainStyledAttributes(attr, R.styleable.PhoneAreaCodeEditView);
+        phoneEdit.setHint(typedArray.getString(R.styleable.PhoneAreaCodeEditView_phone_edit_area_hint_text));
+        phoneEdit.setBackgroundResource(typedArray.getResourceId(R.styleable.PhoneAreaCodeEditView_phone_edit_area_input_bg
+                ,0));
+        areaCodeText.setText(typedArray.getString(R.styleable.PhoneAreaCodeEditView_phone_edit_area_code));
+        Drawable drawable = typedArray.getDrawable(R.styleable.PhoneAreaCodeEditView_phone_edit_area_flag_icon);
+        if (drawable != null)
+            areaCodeText.setCompoundDrawables(getDrawable(drawable),null,null,null);
+        lineView.setVisibility(typedArray.getBoolean(R.styleable.PhoneAreaCodeEditView_phone_edit_area_line_show,true) ? VISIBLE : GONE);
+        typedArray.recycle();
+    }
+
+
+    public void setPhoneEditText(String phone){
+        phoneEdit.setText(phone);
+    }
+
+
+    public String getPhoneEdit(){
+        return phoneEdit.getText().toString().trim();
+    }
+
+    public EditText getInputText(){
+        return phoneEdit;
+    }
+
+    public Drawable getDrawable(Drawable drawable){
+        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
+        return drawable;
+    }
+}

+ 57 - 0
base_library/src/main/res/layout/layout_phone_area_code_edit.xml

@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="utf-8"?>
+<merge xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout"
+    xmlns:app="http://schemas.android.com/apk/res-auto">
+
+
+    <TextView
+        android:id="@+id/area_code"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintTop_toTopOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintEnd_toStartOf="@+id/area_phone_num"
+        android:layout_width="wrap_content"
+        android:textColor="@color/black"
+        android:textSize="@dimen/sp_14"
+        android:layout_height="wrap_content"
+        android:gravity="center" />
+
+    <View
+        app:layout_constraintBottom_toBottomOf="@+id/area_code"
+        app:layout_constraintEnd_toStartOf="@+id/area_phone_num"
+        app:layout_constraintStart_toEndOf="@+id/area_code"
+        app:layout_constraintTop_toTopOf="@+id/area_code"
+        android:layout_width="@dimen/dp_1"
+        android:layout_height="@dimen/dp_0"
+        android:layout_marginTop="@dimen/dp_5"
+        android:layout_marginBottom="@dimen/dp_5"
+        android:layout_marginStart="@dimen/dp_10"
+        android:background="@color/light_grey_bg" />
+
+    <EditText
+        android:id="@+id/area_phone_num"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toEndOf="@+id/area_code"
+        app:layout_constraintTop_toTopOf="parent"
+        android:layout_width="@dimen/dp_0"
+        android:layout_height="@dimen/dp_0"
+        android:background="@color/transparent"
+        android:inputType="phone"
+        android:maxLines="1"
+        android:paddingStart="@dimen/dp_10"
+        android:paddingEnd="@dimen/dp_10"
+        android:singleLine="true"
+        android:textSize="@dimen/sp_14" />
+
+    <View
+        android:id="@+id/area_line"
+        android:layout_width="match_parent"
+        android:layout_height="@dimen/dp_1"
+        app:layout_constraintBottom_toBottomOf="parent"
+        android:background="@color/light_grey_bg"/>
+
+</merge>

+ 6 - 7
base_library/src/main/res/values/attrs.xml

@@ -44,13 +44,12 @@
         </attr>
     </declare-styleable>
 
-    <declare-styleable name="AreaCodeEditView">
-        <attr name="edit_area_title" format="string"/>
-        <attr name="edit_area_hint_text" format="string"/>
-        <attr name="edit_area_code" format="string"/>
-        <attr name="edit_area_input_required" format="boolean"/>
-        <attr name="edit_area_flag_icon" format="reference"/>
-        <attr name="edit_area_input_bg" format="reference"/>
+    <declare-styleable name="PhoneAreaCodeEditView">
+        <attr name="phone_edit_area_hint_text" format="string"/>
+        <attr name="phone_edit_area_code" format="string"/>
+        <attr name="phone_edit_area_flag_icon" format="reference"/>
+        <attr name="phone_edit_area_input_bg" format="reference"/>
+        <attr name="phone_edit_area_line_show" format="boolean"/>
     </declare-styleable>