标签:
需求:
1.数字键盘
2.输入删除,光标要随之移动
3.删除键长按全部清除
4.点击键盘背景有颜色变换效果
效果图:
布局
<?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="48dp"> <TextView android:id="@+id/number_1" style="@style/number_keyboard" android:text="1" /> <TextView android:id="@+id/number_2" style="@style/number_keyboard" android:text="2" /> <TextView android:id="@+id/number_3" style="@style/number_keyboard" android:text="3" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="48dp"> <TextView android:id="@+id/number_4" style="@style/number_keyboard" android:text="4" /> <TextView android:id="@+id/number_5" style="@style/number_keyboard" android:text="5" /> <TextView android:id="@+id/number_6" style="@style/number_keyboard" android:text="6" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="48dp"> <TextView android:id="@+id/number_7" style="@style/number_keyboard" android:text="7" /> <TextView android:id="@+id/number_8" style="@style/number_keyboard" android:text="8" /> <TextView android:id="@+id/number_9" style="@style/number_keyboard" android:text="9" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="48dp"> <ImageView android:id="@+id/number_clear_last" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:padding="10dp" android:src="@drawable/keyboard_delete_img" android:background="@drawable/number_keyboard_selecter"/> <TextView android:id="@+id/number_0" style="@style/number_keyboard" android:text="0" /> <TextView android:id="@+id/number_enter" style="@style/number_keyboard_enter" android:text="进入" android:textColor="@color/white"/> </LinearLayout> </LinearLayout>
<style name="number_keyboard"> <item name="android:layout_width">0dp</item> <item name="android:layout_height">match_parent</item> <item name="android:layout_weight">1</item> <item name="android:textSize">24sp</item> <item name="android:gravity">center</item> <item name="android:background">@drawable/number_keyboard_selecter</item> </style> <style name="number_keyboard_enter"> <item name="android:layout_width">0dp</item> <item name="android:layout_height">match_parent</item> <item name="android:layout_weight">1</item> <item name="android:textSize">24sp</item> <item name="android:gravity">center</item> <item name="android:background">@drawable/number_keyboard_enter_selecter</item> </style>
number_keyboard_selecter
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape> <stroke android:width="0.5dp" android:color="@color/my_middle_gray"></stroke> <solid android:color="@color/my_blue"></solid> </shape> </item> <item android:state_pressed="false"> <shape> <stroke android:width="0.5dp" android:color="@color/my_middle_gray"></stroke> <solid android:color="@color/white"></solid> </shape> </item> </selector>
number_keyboard_enter_selecter
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape> <stroke android:width="0.5dp" android:color="@color/my_middle_gray"></stroke> <solid android:color="@color/white"></solid> </shape> </item> <item android:state_pressed="false"> <shape> <stroke android:width="0.5dp" android:color="@color/my_middle_gray"></stroke> <solid android:color="@color/my_blue"></solid> </shape> </item> </selector>
//显示数字键盘 public void showNumberKeyboard(){ View view = LayoutInflater.from(getActivity()).inflate(R.layout.number_keyboard, null); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.DialogTheme); builder.setView(view); final AlertDialog keyDialog = builder.create(); Window window = keyDialog.getWindow(); window.setGravity(Gravity.BOTTOM); window.setWindowAnimations(R.style.dialog_animation); window.getDecorView().setPadding(0, 0, 0, 0); WindowManager.LayoutParams lp = window.getAttributes(); lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; window.setAttributes(lp); keyDialog.show(); //数字键盘点击监听 number_1 = (TextView) view.findViewById(R.id.number_1); number_2 = (TextView) view.findViewById(R.id.number_2); number_3 = (TextView) view.findViewById(R.id.number_3); number_4 = (TextView) view.findViewById(R.id.number_4); number_5 = (TextView) view.findViewById(R.id.number_5); number_6 = (TextView) view.findViewById(R.id.number_6); number_7 = (TextView) view.findViewById(R.id.number_7); number_8 = (TextView) view.findViewById(R.id.number_8); number_9 = (TextView) view.findViewById(R.id.number_9); number_0 = (TextView) view.findViewById(R.id.number_0); number_enter = (TextView) view.findViewById(R.id.number_enter); number_clear_last = (ImageView) view.findViewById(R.id.number_clear_last); number_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String roomInput = myCourse_roomId_input.getText().toString(); myCourse_roomId_input.setText(roomInput+number_1.getText().toString()); myCourse_roomId_input.setSelection(roomInput.length()+1); } }); number_2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String roomInput = myCourse_roomId_input.getText().toString(); myCourse_roomId_input.setText(roomInput+number_2.getText().toString()); myCourse_roomId_input.setSelection(roomInput.length()+1); } }); number_3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String roomInput = myCourse_roomId_input.getText().toString(); myCourse_roomId_input.setText(roomInput+number_3.getText().toString()); myCourse_roomId_input.setSelection(roomInput.length()+1); } }); number_4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String roomInput = myCourse_roomId_input.getText().toString(); myCourse_roomId_input.setText(roomInput+number_4.getText().toString()); myCourse_roomId_input.setSelection(roomInput.length()+1); } }); number_5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String roomInput = myCourse_roomId_input.getText().toString(); myCourse_roomId_input.setText(roomInput+number_5.getText().toString()); myCourse_roomId_input.setSelection(roomInput.length()+1); } }); number_6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String roomInput = myCourse_roomId_input.getText().toString(); myCourse_roomId_input.setText(roomInput+number_6.getText().toString()); myCourse_roomId_input.setSelection(roomInput.length()+1); } }); number_7.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String roomInput = myCourse_roomId_input.getText().toString(); myCourse_roomId_input.setText(roomInput+number_7.getText().toString()); myCourse_roomId_input.setSelection(roomInput.length()+1); } }); number_8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String roomInput = myCourse_roomId_input.getText().toString(); myCourse_roomId_input.setText(roomInput+number_8.getText().toString()); myCourse_roomId_input.setSelection(roomInput.length()+1); } }); number_9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String roomInput = myCourse_roomId_input.getText().toString(); myCourse_roomId_input.setText(roomInput+number_9.getText().toString()); myCourse_roomId_input.setSelection(roomInput.length()+1); } }); number_0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String roomInput = myCourse_roomId_input.getText().toString(); //0不能在第一位 if(null != roomInput && !"".equals(roomInput)){ myCourse_roomId_input.setText(roomInput+number_0.getText().toString()); myCourse_roomId_input.setSelection(roomInput.length()+1); } } }); number_clear_last.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String roomIdInput = myCourse_roomId_input.getText().toString(); if(roomIdInput.length()>0){ myCourse_roomId_input.setText(roomIdInput.substring(0,roomIdInput.length()-1)); myCourse_roomId_input.setSelection(roomIdInput.length()-1); } } }); number_clear_last.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { myCourse_roomId_input.setText(""); return false; } }); number_enter.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final String roomId = myCourse_roomId_input.getText().toString(); if(TextUtils.isEmpty(roomId)){ Toast.makeText(getActivity(),"请输入房间号",Toast.LENGTH_SHORT).show(); }else{ LiveRoomAPI.getTopicInfo(roomId, new BaseCallBack<TopicInfoResponseEntity>() { @Override public void onSuccess(Response<TopicInfoResponseEntity> response) { //跳转到直播间 Intent intent = new Intent(getActivity(), TopicInfoActivity.class); intent.putExtra(IntentKey.topicId, roomId); startActivity(intent); keyDialog.dismiss(); } @Override public void onFailure(StateException exception) { //弹出对话框 com.xuehu365.xuehu.ui.widget.AlertDialog alertDialog = new com.xuehu365.xuehu.ui.widget.AlertDialog(getActivity()).builder(); alertDialog.setMsg("您输入的房号有误请重新输入!"); alertDialog.setPositiveButton("好的", new View.OnClickListener() { @Override public void onClick(View v) { myCourse_roomId_input.setText(""); myCourse_roomId_clear.setVisibility(View.GONE); } }); alertDialog.show(); } }); } } }); }
标签:
原文地址:http://www.cnblogs.com/anni-qianqian/p/5836114.html