前言:自己写了一个小控件,感觉用的好像还可以,记录下来以后备用~~
1.先准备四个图片放在res/mipmap-xhdpi文件夹下
2.在res/drawable文件夹下新建btn_sp_add_selector.xml文件
如果没有drawable文件夹可以自己新建
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@mipmap/btn_ps_add_active">
</item>
<item android:drawable="@mipmap/btn_ps_add_normal">
</item>
</selector>
在res/drawable文件夹下新建btn_sp_plus_selector.xml文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@mipmap/btn_ps_plus_active">
</item>
<item android:drawable="@mipmap/btn_ps_plus_normal">
</item>
</selector>
3.在res/layout文件夹下创建选择控件布局文件view_pickspeed_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_pickspeed_item"
android:layout_width="180dp"
android:layout_height="36dp"
android:paddingLeft="10dp"
android:paddingRight="4dp">
<TextView
android:id="@+id/tv_pickspeed_item"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:gravity="center" />
<Button
android:id="@+id/btndown_pickspeed_item"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_toRightOf="@+id/tv_pickspeed_item"
android:layout_centerVertical="true"
android:background="@drawable/btn_sp_plus_selector"/>
<EditText
android:id="@+id/et_pickspeed_item"
android:layout_width="match_parent"
android:layout_height="36dp"
android:layout_toLeftOf="@+id/btnup_pickspeed_item"
android:layout_toRightOf="@+id/btndown_pickspeed_item"
android:layout_alignTop="@+id/tv_pickspeed_item"
android:background="@android:color/transparent"
android:gravity="center"
android:lines="1"
android:maxLength="2"/>
<Button
android:id="@+id/btnup_pickspeed_item"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_centerVertical="true"
android:gravity="center"
android:layout_alignParentRight="true"
android:background="@drawable/btn_sp_add_selector"/>
</RelativeLayout>
4.在res/values文件夹下创建样式文件attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="PickspeedItemView">
<attr name="title_text" format="string"/>//TextView要显示的字符串
<attr name="title_color" format="color"/>//TextView文字颜色
<attr name="title_size" format="dimension"/>//TextView字符大小
<attr name="speed_text" format="string"/>//EditText要显示的字符串
<attr name="speed_size" format="dimension"/>//EditText字符大小
<attr name="speed_color" format="color"/>//EditText文字颜色
<attr name="speed_hint_color" format="color"/>//EditText提示文字颜色
<attr name="speed_focus" format="boolean"/>//EditText是否可以编辑
<attr name="item_width" format="dimension"/>//设置控件宽度
<attr name="item_height" format="dimension"/>//设置控件高度
<attr name="btn_width" format="dimension"/>//按钮宽度
<attr name="btn_height" format="dimension"/>//按钮高度
<attr name="accur_value" format="integer"/>//上下浮动的值
<attr name="min_value" format="integer"/>
<attr name="max_value" format="integer"/>
<attr name="speedType">
<enum name="number" value="2" />
<enum name="decimal" value="8192" />
</attr>
<attr name="enable" format="boolean"></attr>
</declare-styleable>
</resources>
5.创建PickspeedView类
package com.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class PickspeedView extends RelativeLayout{
private RelativeLayout rlContent;
private TextView tvTitle;
private EditText etSpeed;
private Button btnAddSpeed;
private Button btnLessSpeed;
private int DEFAULT_TITLE_SIZE = 30;
private int DEFAULT_TITLE_COLOR = Color.GRAY;
private int DEFAULT_SPEED_SIZE = 30;
private int DEFAULT_SPEED_COLOR = Color.BLACK;
private int DEFAULT_SPEED_HINT_COLOR = Color.GRAY;
private boolean DEFAULT_SPEED_FOCUS = true;
private int DEFAULT_ITEM_WIDTH=96;
private int DEFAULT_ITEM_HEIGHT=48;
private int DEFAULT_BUTTON_WIDTH=24;
private int DEFAULT_BUTTON_HEIGHT=24;
private int DEFAULT_ACCUR_VALUE=1;
private int DEFAULT_MIN_VALUE=0;
private int DEFAULT_MAX_VALUE=50;
private String titleText;
private float titleSize;
private int titleColor;
private float speedSize;
private int speedColor;
private int speedHintColor;
private String speedText;
private boolean isSpeedFocus;
private float itemWidth;
private float itemHeight;
private float btnWidth;
private float btnHeight;
private int accurVaule;
private int minValue;
private int maxValue;
private int speedType;
private boolean enable;
private Context context;
private SpeedDataChangeCallback speedDataChangeCallback;
public interface SpeedDataChangeCallback {
void speedChange(String speed);
}
public void setSpeedDataChangeCallback(SpeedDataChangeCallback speedDataChangeCallback) {
this.speedDataChangeCallback = speedDataChangeCallback;
}
public PickspeedView(Context context, AttributeSet attrs) {
super(context, attrs);
//加载属性值
initAttrs(context, attrs);
//加载布局
initView(context);
this.context = context;
}
private void initAttrs(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PickspeedItemView);
titleText = typedArray.getString(R.styleable.PickspeedItemView_title_text);
titleSize = typedArray.getDimension(R.styleable.PickspeedItemView_title_size, DEFAULT_TITLE_SIZE);
titleColor = typedArray.getColor(R.styleable.PickspeedItemView_title_color, DEFAULT_TITLE_COLOR);
speedText = typedArray.getString(R.styleable.PickspeedItemView_speed_text);
speedSize = typedArray.getDimension(R.styleable.PickspeedItemView_speed_size, DEFAULT_SPEED_SIZE);
speedColor = typedArray.getColor(R.styleable.PickspeedItemView_speed_color, DEFAULT_SPEED_COLOR);
speedHintColor = typedArray.getColor(R.styleable.PickspeedItemView_speed_hint_color, DEFAULT_SPEED_HINT_COLOR);
isSpeedFocus = typedArray.getBoolean(R.styleable.PickspeedItemView_speed_focus, DEFAULT_SPEED_FOCUS);
itemWidth = typedArray.getDimension(R.styleable.PickspeedItemView_item_width,DEFAULT_ITEM_WIDTH);
itemHeight = typedArray.getDimension(R.styleable.PickspeedItemView_item_height,DEFAULT_ITEM_HEIGHT);
btnWidth= typedArray.getDimension(R.styleable.PickspeedItemView_btn_width,DEFAULT_BUTTON_WIDTH);
btnHeight = typedArray.getDimension(R.styleable.PickspeedItemView_btn_height,DEFAULT_BUTTON_HEIGHT);
accurVaule = typedArray.getInt(R.styleable.PickspeedItemView_accur_value,DEFAULT_ACCUR_VALUE);
minValue = typedArray.getInt(R.styleable.PickspeedItemView_min_value,DEFAULT_MIN_VALUE);
maxValue = typedArray.getInt(R.styleable.PickspeedItemView_max_value,DEFAULT_MAX_VALUE);
speedType = typedArray.getInt(R.styleable.PickspeedItemView_speedType,2);
enable = typedArray.getBoolean(R.styleable.PickspeedItemView_enable,true);
typedArray.recycle();
}
private void initView(Context context) {
LayoutInflater.from(context).inflate(R.layout.view_pickspeed_item, this);// 加载布局
rlContent = (RelativeLayout)findViewById(R.id.content_pickspeed_item);
tvTitle = (TextView) findViewById(R.id.tv_pickspeed_item);
etSpeed = (EditText) findViewById(R.id.et_pickspeed_item);// 获取控件
btnAddSpeed = (Button) findViewById(R.id.btnup_pickspeed_item);
btnLessSpeed = (Button)findViewById(R.id.btndown_pickspeed_item);
//RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams)rlContent.getLayoutParams();
//linearParams.height = (int)itemHeight;
//linearParams.width = (int)itemWidth;
//rlContent.setLayoutParams(linearParams);
tvTitle.setText(titleText);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,titleSize);
//如果不设置这个文字会变得非常大
tvTitle.getPaint().setTextSize(titleSize);
tvTitle.setTextColor(titleColor);
// etSpeed.setHeight((int)itemHeight);
//etSpeed.setWidth((int)(itemWidth-btnWidth));
etSpeed.setText(speedText);
etSpeed.setTextSize(TypedValue.COMPLEX_UNIT_SP,speedSize);
//如果不设置这个文字会变得非常大
etSpeed.getPaint().setTextSize(speedSize);
etSpeed.setTextColor(speedColor);
etSpeed.setHintTextColor(speedHintColor);
if(!isSpeedFocus){
etSpeed.setFocusable(false);
}
etSpeed.setInputType(speedType);
// btnAddSpeed.setHeight((int)btnHeight);
//btnAddSpeed.setWidth((int)btnWidth);
// btnLessSpeed.setHeight((int)btnHeight);
// btnLessSpeed.setWidth((int)btnWidth);
btnAddSpeed.setOnClickListener(new BtnListener());
btnLessSpeed.setOnClickListener(new BtnListener());
}
public void setViewEnable(boolean enable){
this.enable = enable;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//返回true则拦截子控件所有点击事件,如果enable为true,则需返回false
return !enable;
}
public class BtnListener implements OnClickListener{
@Override
public void onClick(View view) {
int value = Integer.parseInt(etSpeed.getText().toString().trim());
switch (view.getId()){
case R.id.btnup_pickspeed_item:
if(value < maxValue){
value = value + accurVaule;
if(value>maxValue) value = maxValue;
setContentText(value+"");
}else if(value>maxValue){
value = maxValue;
setContentText(value+"");
}
break;
case R.id.btndown_pickspeed_item:
if(value > minValue){
value = value-accurVaule;
if(value<minValue) value = minValue;
setContentText(value+"");
}else if(value<minValue){
value = minValue;
setContentText(value+"");
}
break;
}
}
}
//设置EditText值
public void setContentText(String str){
etSpeed.setText(str);
if(speedDataChangeCallback !=null){
speedDataChangeCallback.speedChange(str);
}
}
//获取EditText值
public String getContentText(){
return etSpeed.getText().toString().trim();
}
}
6.使用 activity_setting.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<com.view.PickspeedView
android:id="@+id/psv_hortal_speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:accur_value="1"
app:max_value="10"
app:min_value="1"
app:speedType="number"
app:speed_size="14sp"
app:speed_text="1"
app:title_size="14sp"
app:title_text="水平速度:"/>
<com.view.PickspeedView
android:id="@+id/psv_hortal_step"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:accur_value="1"
app:max_value="10"
app:min_value="1"
app:speedType="number"
app:speed_size="14sp"
app:speed_text="1"
app:title_size="14sp"
app:title_text="水平步长:"/>
</LinearLayout>
package com.activity;
public class SettingActivity extends Activity {
private PickspeedView psvHortalSpeed;
private PickspeedView psvHortalStep;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
initView();
}
private void initView(){
psvHortalSpeed = (PickspeedView)findViewById(R.id.psv_hortal_speed);
psvHortalSpeed.setSpeedDataChangeCallback(new PickspeedView.SpeedDataChangeCallback() {
@Override
public void speedChange(String speed) {
Log.d("TAG","速度改变了"+speed);
}
});
psvHortalStep = (PickspeedView)findViewById(R.id.psv_hortal_step);
Log.d("TAG","当前速度值:"+psvHortalSpeed.getContentText());
Log.d("TAG","当前步长值:"+psvHortalStep.getContentText());
}
}
7.最终样式
点击加减按钮,可以修改速度值,也可以点击直接输入速度值