加减器自定义
作者:互联网
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bawei.moni.R;
/**
-
@Author:苏羡c
-
@E-mail:
-
@Date:2019/4/4 11:37
-
@Description:描述信息
*/
public class jiajianqi extends LinearLayout {private TextView jian,num,jia;
public jiajianqi(Context context) {
this(context,null);
}public jiajianqi(Context context, AttributeSet attrs) {
this(context, attrs,-1);
}public jiajianqi(Context context,AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
initListen();
}private void initListen() {
jia.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sum();
}
});
jian.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
kejian();
}
});}
private void kejian() {
String s = num.getText().toString();
int i;
if (!TextUtils.isEmpty(s)&&!s.contains(".")){
i = Integer.parseInt(s);
i–;
if (i<0){
i=0;
Toast.makeText(getContext(), “不能为0”, Toast.LENGTH_SHORT).show();
}
num.setText(i+"");
}
}private void sum() {
String s = num.getText().toString();
int i;
if(!TextUtils.isEmpty(s) && !s.contains(".")){
i = Integer.parseInt(s);
i++;
num.setText(i+"");
}
}private void init(Context context) {
View view = inflate(context, R.layout.jiajianqi_xml, this);
jian = findViewById(R.id.jian);
num = findViewById(R.id.num);
jia = findViewById(R.id.jia);}
public void setNumS(int nums){
if (nums>0){
num.setText(nums+"");
}else {
Toast.makeText(getContext(), “不能为0”, Toast.LENGTH_SHORT).show();
}
}
}
标签:自定义,加减器,void,num,context,import,android,public 来源: https://blog.csdn.net/weixin_43241892/article/details/89060098