0
点赞
收藏
分享

微信扫一扫

短按数字加减,长按数字清零

洛茄 2023-11-24 阅读 44


数字加减运算,长按数字会归0。

####1、layout布局

短按数字加减,长按数字清零_ide

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lumeng.myapplication.MainActivity">


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/testview_id"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="0"
            android:textSize="80dp"/>
    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/bt_add"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/add"
            android:textSize="40dp"/>

        <Button
            android:id="@+id/bt_reduce"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/reduce"
            android:textSize="40dp"/>
    </LinearLayout>
</RelativeLayout>

####2、运算处理

package com.example.lumeng.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnLongClickListener, View.OnClickListener {
    TextView txv;
    Button bt_add;
    Button bt_reduce;
    int number=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txv=(TextView) findViewById(R.id.testview_id);
        bt_add=(Button) findViewById(R.id.bt_add);
        bt_reduce=(Button) findViewById(R.id.bt_reduce);

        txv.setOnLongClickListener(this);
        bt_add.setOnClickListener(this);
        bt_reduce.setOnClickListener(this);
    }

    @Override
    public boolean onLongClick(View view) {
        number=0;
        txv.setText("0");
        return true;
    }

    @Override
    public void onClick(View view) {
        int id=view.getId();

        switch(id){
            case R.id.bt_add:
                txv.setText(String.valueOf(++number));
                break;
            case R.id.bt_reduce:
                txv.setText(String.valueOf(--number));
                break;
            default:
                break;
        }

    }
}

文献参考:
android app开发入门 施威铭 编著

本人郑重声明,本博客所著文章、图片版权归权利人持有,本博只做学习交流分享所用,不做任何商业用途。访问者可將本博提供的內容或服务用于个人学习、研究或欣赏,不得用于商业使用。同時,访问者应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人的合法权利;如果用于商业用途,须征得相关权利人的书面授权。若以上文章、图片的原作者不愿意在此展示內容,请及时通知在下,將及时予以刪除。


举报

相关推荐

0 条评论