[Android Studio]1.2计数器

所有要改的代码如下: 

MainActivity代码:

package com.example.code02;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private int count=0;
    private TextView tvCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnShowToast = findViewById(R.id.btnShowToast);
        btnShowToast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "Hello World!",Toast.LENGTH_SHORT).show();
            }
        });

        tvCount=findViewById(R.id.tvCount);
        Button btnCount = findViewById(R.id.btnCount);
        btnCount . setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvCount.setText(Integer.toString(++count)) ;
            }
        }) ;

    }
}

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnShowToast"
        android:text="Show Toast"
        android:textColor="@android:color/white"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tvCount"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="0"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="160sp"
        android:textColor="@color/colorAccent"

       />

    <Button
        android:id="@+id/btnCount"
        android:text="Count"
        android:textColor="@android:color/white"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />



</LinearLayout>

colors:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="background_color">#009688</color>
    <color name="white_grey">#EEEEEE</color>
    <color name="button_selector_color">#9E9E9E</color>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#004A57</color>
    <color name="colorAccent">#C550DA</color>
</resources>

strings部分我没动,但是代码可以在这里再改进,不过不影响功能

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 可以使用一个整型变量来记录按钮被点击的次数,每次点击按钮时将该变量加1,然后将该变量的值显示在界面上即可。具体实现可以参考以下步骤: 1. 在布局文件中添加一个按钮和一个TextView控件,用于显示计数器的值。 2. 在Activity中定义一个整型变量count,用于记录按钮被点击的次数。 3. 在按钮的点击事件中,将count加1,并将count的值显示在TextView控件中。 4. 如果需要在应用退出后仍能保留计数器的值,可以使用SharedPreferences来保存count的值。 示例代码如下: 布局文件: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击计数"/> <TextView android:id="@+id/tv_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn_count" android:textSize="24sp"/> </RelativeLayout> ``` Activity代码: ``` public class MainActivity extends AppCompatActivity { private Button mBtnCount; private TextView mTvCount; private int mCount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBtnCount = findViewById(R.id.btn_count); mTvCount = findViewById(R.id.tv_count); // 从SharedPreferences中读取计数器的值 SharedPreferences sp = getPreferences(Context.MODE_PRIVATE); mCount = sp.getInt("count", ); mTvCount.setText(String.valueOf(mCount)); mBtnCount.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mCount++; mTvCount.setText(String.valueOf(mCount)); // 将计数器的值保存到SharedPreferences中 SharedPreferences sp = getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putInt("count", mCount); editor.apply(); } }); } } ``` ### 回答2: Android Studio按钮计数器可以通过以下步骤实现: 1. 创建一个新的Android Studio项目。 2. 在XML布局文件中添加一个TextView用于显示计数。 ```xml <TextView android:id="@+id/counterTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" /> ``` 3. 在Java文件中找到计数器按钮,并为其添加点击事件。 ```java Button counterButton = findViewById(R.id.counterButton); counterButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 获取计数TextView TextView counterTextView = findViewById(R.id.counterTextView); // 获取当前计数值 int count = Integer.parseInt(counterTextView.getText().toString()); // 增加计数值 count++; // 更新计数TextView counterTextView.setText(String.valueOf(count)); } }); ``` 在按钮的点击事件中,首先获取当前计数TextView的文本值,然后将其转换为整数类型。接着对计数值进行增加操作,并将更新后的计数值重新设置到计数TextView中。 4. 运行项目,点击计数器按钮,可以看到计数值每次增加1。 通过上述步骤,我们成功实现了一个简单的Android Studio按钮计数器。 ### 回答3: 在Android Studio中实现按钮计数器的功能可以通过以下步骤完成: 1. 首先,在你的项目中创建一个新的按钮,用于计数器的展示和操作。 2. 在布局文件中添加一个Button控件,并为它设置一个唯一的ID。 3. 在Activity类中获取该Button控件的实例,并为它设置一个点击事件的监听器。 4. 在监听器的回调方法中,定义一个整型变量用于保存按钮的点击次数,并初始化为0。 5. 每次按钮被点击时,将计数变量加1,并更新按钮的文本显示为当前计数值。 以下是一个简单的示例代码,实现了一个按钮计数器的功能: ```java public class MainActivity extends AppCompatActivity { private Button counterButton; private int count = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); counterButton = findViewById(R.id.counter_button); counterButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { count++; counterButton.setText("点击次数:" + count); } }); } } ``` 通过以上步骤,每点击一次按钮,计数器的值就会加1,并在按钮上显示当前的计数值。你可以根据自己的需求修改布局文件和按钮的样式,实现更加丰富的按钮计数器效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

地球原来是这个样子啊我去

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值