Dialog: Manually enter information

To imitate "ofo" to manually enter the bicycle ID, the interface may be a little ugly, the general function:

1. If the id number entered by the customer exceeds 12 digits, the user will be prompted and can no longer enter

2. When the string is empty, "x" disappears, and the customer clicks "x" to clear the content in editText

3. The interface calls back the content in editText and performs corresponding operations

.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/popwindow_top"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/addid_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="11dp"
            android:layout_marginTop="11dp"
            android:text="Add device ID"
            android:textColor="@color/blue_deep"
            android:textSize="22sp"
            android:textStyle="bold" />
    </LinearLayout>

    <ImageView style="@style/iv" />

    <RelativeLayout
        android:background="@color/white"
        android:padding="11dp"
        android:layout_width="match_parent"
        android:layout_height="66dp">

        <EditText
            android:id="@+id/addid_et"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/addid_bg"
            android:ems="12"
            android:gravity="center"
            android:hint="Click here to enter the device ID"
            android:maxLength="12"
            android:maxLines="1"
            android:textColor="#ff1d1d1d"
            android:textColorHint="#ff666666"
            android:textSize="22sp" />

        <ImageView
            android:id="@+id/addid_iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="22dp"
            android:background="@drawable/cross"
            android:visibility="gone" />

    </RelativeLayout>

    <ImageView style="@style/iv" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/addid_ok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_bottomleft_bg"
            android:text="@android:string/ok"
            android:textColor="@drawable/textcolor"
            android:textSize="22sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/gray_shallow" />

        <Button
            android:id="@+id/addid_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_bottomright_bg"
            android:text="@android:string/cancel"
            android:textColor="@drawable/textcolor"
            android:textSize="22sp" />
    </LinearLayout>


</LinearLayout>

.java

public class DialogAddId {
    private Dialog iddialog;
    private EditText id;
    private ImageView iv;

    public DialogAddId(Context context, final putIdListener listener) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_id, null);
        id = (EditText) view.findViewById(R.id.addid_et);
        iv = (ImageView) view.findViewById(R.id.addid_iv);
        id.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                Logs.d("变化前:" + s + "   " + s.length());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                Logs.w("变化中:" + s + "   " + s.length());
                if (s.length() == 12) {
                    ToastUtil.showShort("The length of the device ID cannot exceed 12 digits");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    iv.setVisibility(View.VISIBLE);
                } else {
                    iv.setVisibility(View.GONE);
                }
// Logs.e("After change:" + s + " " + s.length());
            }
        });
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                id.setText("");
            }
        });
        view.findViewById(R.id.addid_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                assertlog.dismiss();
            }
        });
        view.findViewById(R.id.addid_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.putId(id.getText().toString());
                assertlog.dismiss();
            }
        });

        DisplayMetrics dm = context.getResources().getDisplayMetrics();// screen width
        iddialog = new Dialog(context, R.style.dialog);
        // Setting the return key is invalid
        iddialog.setCancelable(false);
        iddialog.getWindow().setGravity(Gravity.CENTER_VERTICAL);
        iddialog.setContentView(view, new LinearLayout.LayoutParams(
                dm.widthPixels * 7 / 8,
                LinearLayout.LayoutParams.MATCH_PARENT));
        iddialog.show();
    }

    public interface putIdListener {
        public void putId(String id);
    }


}

call dialog

DialogAddId idDialog=new DialogAddId(getActivity(), new DialogAddId.putIdListener() {
                        @Override
                        public void putId(String id) {
                            Logs.v("The input device ID is: "+id);
                        }
                    });


renderings





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324504129&siteId=291194637