第一种方法:
attrs:
<resources>
<declare-styleable name="MyAddAndRemoveView">
<attr name="textColor" format="string"></attr>
<attr name="textSize" format="integer"></attr>
</declare-styleable>
</resources>
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/Add_Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add" />
<TextView
android:id="@+id/Num_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="memeda" />
<Button
android:id="@+id/Remove_Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remove" />
</LinearLayout>
代码:
public class MyAddAndRemoveView extends LinearLayout implements View.OnClickListener {
private Button mAdd, mRemove;
private TextView mNumText;
public MyAddAndRemoveView(Context context, AttributeSet attrs) {
super(context, attrs);
View.inflate(context, R.layout.add_remove_layout, this);
initViews();
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyAddAndRemoveView);
String color = array.getString(R.styleable.MyAddAndRemoveView_textColor);
int textSize = array.getInteger(R.styleable.MyAddAndRemoveView_textSize, 0);
array.recycle();
mNumText.setTextSize(textSize);
mNumText.setTextColor(Color.parseColor(color));
}
private void initViews() {
mAdd = findViewById(R.id.Add_Btn);
mAdd.setOnClickListener(this);
mRemove = findViewById(R.id.Remove_Btn);
mRemove.setOnClickListener(this);
mNumText = findViewById(R.id.Num_Text);
}
public void setNum(int number) {
mNumText.setText(number + "");
}
@Override
public void onClick(View v) {
String trim = mNumText.getText().toString().trim();
int number = Integer.parseInt(trim);
switch (v.getId()) {
case R.id.Add_Btn:
number++;
mNumText.setText(number + "");
break;
case R.id.Remove_Btn:
if (number > 0) {
number--;
mNumText.setText(number + "");
}
break;
}
}
}
MainActivity.xml:
<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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.example.fu_demo.weight.MyAddAndRemoveView
android:id="@+id/MyAdd_View"
android:layout_width="wrap_content"
android:layout_height="80dp"
app:textColor="#d13b3b"
app:textSize="18" />
<TextView
android:id="@+id/Get_Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="memeda"/>
</LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private MyAddAndRemoveView MyAdd_View;
private TextView Get_Number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
MyAdd_View.setNum(10);
}
private void initView() {
MyAdd_View = (MyAddAndRemoveView) findViewById(R.id.MyAdd_View);
Get_Number = (TextView) findViewById(R.id.Get_Number);
}
}
第二种方法:
布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/jian"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="20sp"
android:gravity="center"
android:text="-" />
<TextView
android:id="@+id/count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="20sp"
android:gravity="center"
android:text="1" />
<Button
android:id="@+id/jia"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="20sp"
android:gravity="center"
android:text="+" />
</LinearLayout>
主要代码:
public class CustomView extends LinearLayout implements View.OnClickListener {
public Button jian;
public TextView tvCount;
public Button jia;
public int count;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.custom, this, true);
jian = findViewById(R.id.jian);
tvCount = findViewById(R.id.count);
jia = findViewById(R.id.jia);
jian.setOnClickListener(this);
jia.setOnClickListener(this);
}
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onClick(View view) {
jia.setOnClickListener(this);
jian.setOnClickListener(this);
switch (view.getId()) {
case R.id.jia:
count++;
tvCount.setText(count + "");
if (inter != null) {
inter.setnumber(count);
}
break;
case R.id.jian:
if (count > 1) {
count--;
tvCount.setText(count + "");
if (inter != null) {
inter.setnumber(count);
}
} else {
Toast.makeText(getContext(), "参数不能小于1", Toast.LENGTH_SHORT).show();
}
break;
}
}
public MyInter inter;
public void setInter(MyInter inter) {
this.inter = inter;
}
public interface MyInter {
public void setnumber(int index);
}
}