模仿"ofo"手动输入自行车ID,界面可能丑了了点,大致功能:
1.如果客户输入的id号超过12位提示用户并且不可以再输入
2.当字符串为空时,”x“消失,客户点击”x“能清空editText里面的内容
3.接口回调editText里面的内容并作相应的操作
.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="添加设备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="点此输入设备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("设备ID的长度不能超过12位"); } } @Override public void afterTextChanged(Editable s) { if (s.length() > 0) { iv.setVisibility(View.VISIBLE); } else { iv.setVisibility(View.GONE); } // Logs.e("变化后:" + 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) { iddialog.dismiss(); } }); view.findViewById(R.id.addid_ok).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listener.putId(id.getText().toString()); iddialog.dismiss(); } }); DisplayMetrics dm = context.getResources().getDisplayMetrics();// 屏幕宽度 iddialog = new Dialog(context, R.style.dialog); // 设置返回键无效 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); } }
调用dialog
DialogAddId idDialog=new DialogAddId(getActivity(), new DialogAddId.putIdListener() { @Override public void putId(String id) { Logs.v("输入的设备ID为:"+id); } });
效果图