public class AddDecView extends LinearLayout { private TextView addtext; private TextView dectext; private EditText etshow; private int count = 0; public AddDecView(Context context) { this(context, null); } public AddDecView(Context context, AttributeSet attrs) { this(context, attrs, 0 ); } public AddDecView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); View view = View.inflate(context, R.layout.view_add, this); addtext = (TextView) view.findViewById(R.id.add_text); dectext = (TextView) view.findViewById(R.id.dec_text); etshow = (EditText) view.findViewById(R.id.ed_show); etshow.setText(count + ""); addtext.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { count++; etshow.setText(count + ""); } }); dectext.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (count>0){ count--; } etshow.setText(count + ""); } }); } public int getCount() { return count; } public void setCount(int i) { this.count = count; etshow.setText(count + ""); } }public class MainActivity extends AppCompatActivity { private AddDecView adv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adv= (AddDecView) findViewById(R.id.adv); adv.setCount(0); } }<TextView android:id="@+id/dec_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#aba9a9" android:padding="10dp" android:text="-" /> <TextView android:id="@+id/add_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="#aba9a9" android:padding="10dp" android:text="+" /> <EditText android:textColor="#05faee" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:gravity="center" android:background="@null" android:text="0" android:id="@+id/ed_show" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/add_text" android:layout_toRightOf="@+id/dec_text" android:inputType="number" />
简单的混合自定义View
最新推荐文章于 2022-02-16 23:26:28 发布