您的位置:首页 > 其它

安卓点击加减号实现数字加减功能

2017-12-19 17:31 246 查看
直接上代码

layout代码:

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:onClick="iv_1"
android:layout_gravity="center"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="match_parent">
<ImageView
android:layout_centerInParent="true"
android:src="@drawable/p2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:layout_gravity="center"
android:layout_weight="5"
android:layout_width="0dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:paddingTop="1dp"
android:paddingBottom="1dp"
android:gravity="center"
android:layout_centerInParent="true"
android:text="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</Relat
9e46
iveLayout>

<RelativeLayout
android:onClick="iv_2"
android:layout_gravity="center"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="match_parent">
<ImageView
android:layout_centerInParent="true"
android:src="@drawable/p1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

</LinearLayout>


class代码:

public void iv_1(View view){
textView1=(TextView)findViewById(R.id.textView1);
num1=Integer.parseInt(textView1.getText().toString());
if(num1>1){
num1-=1;
}

textView1.setText(Integer.toString(num1));

}
public void iv_2(View view){
textView1=(TextView)findViewById(R.id.textView1);
num1=Integer.parseInt(textView1.getText().toString());
if(num1<999){
num1+=1;
}

textView1.setText(Integer.toString(num1));

}

效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: