手机app开发2-统计点击次数的程序

前言

前面已经把视图弄好了,接下来我们弄按键

    <Button
        android:id="@+id/MyButton"
        android:layout_width="210dp"
        android:layout_height="118dp"
        android:text="点击我"
        tools:layout_editor_absoluteX="98dp"
        tools:layout_editor_absoluteY="407dp" />

在着界面添加

修改这个界面的内容

    private int count=0;//这个添加的位置注意看看




 TextView textView = findViewById(R.id.initview);

        Button button = findViewById(R.id.MyButton);

 if(button!=null)
        button.setOnClickListener(v -> {
            count++;
textView.setText("成功点击"+count+"次");
        });

然后成功实现

在    button.setOnClickListener(v -> {
            count++;
textView.setText("成功点击"+count+"次");
        });

这个函数体里面可以添加触发事件

成功实现