【问题标题】:Ticker Button That Needs Duplicating需要复制的代码按钮
【发布时间】:2013-02-19 19:54:07
【问题描述】:

我得到了如下所示的按钮代码,但它只有一个按钮和一个 textview 我的项目需要的意思是需要有两个或多个按钮可以计入单独的系统但使用相同的代码。我将在下面包含主要代码,但任何在系统中添加更多功能的帮助都非常受欢迎!

主代码

package com.example.counter;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {
    // Private member field to keep track of the count
    private static int mCount = 0;

    private TextView countTextView;
    private Button countButton;
    public static final String PREFS_NAME = "com.example.myApp.mCount";
    private SharedPreferences settings = null;
    private SharedPreferences.Editor editor = null;

    /** ADD THIS METHOD **/
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);  
      setContentView(R.layout.main);
      countTextView = (TextView) findViewById(R.id.TextViewCount);
      countButton = (Button) findViewById(R.id.ButtonCount);

      countButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              mCount++;
              countTextView.setText("Count: " + mCount);
              editor = settings.edit(); 
              editor.putInt("mCount", mCount);
              editor.commit();
          }
      });
    settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);


     }

    @Override
    public void onPause() {
      super.onPause();  
    }

    @Override
    public void onResume() {
      super.onResume();  
      mCount = settings.getInt("mCount", 0);
      countTextView.setText("Count: " + mCount);
    }
    }

【问题讨论】:

  • 所以添加您需要的额外按钮。如果你真的需要问一个简单的问题,请阅读一些 Android 教程。
  • 不是那么简单,我尝试添加按钮,即使我更改代码也不起作用
  • 我不完全确定你的问题,你需要有 3 个按钮,每个按钮都具有相同的 onClickListener,3 个不同活动中的按钮具有相同的 onClickListener,还是完全不同的?
  • 这 3 个按钮需要在相同的代码上运行,但每个按钮的计数不同 - 您可以按一个而不影响其他按钮
  • 我不完全确定“相同的代码但每个计数不同”是什么意思,但我相信您想要 3 个具有不同 onClickListeners 的按钮?即,如果我单击按钮 1 应该会发生什么,如果我单击按钮 2 应该会发生一些不同的事情,而按钮 3 应该会做一些不同的事情吗?

标签: java android multithreading ticker


【解决方案1】:

在您的 xml 中创建 3 个按钮,假设它们的 id 是 ButtonCount、ButtonCount2 和 ButtonCount3 以及声明的 countButton2 和 countButton3。然后将它们初始化如下:

  countButton = (Button) findViewById(R.id.ButtonCount);

  countButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
          mCount++;
          countTextView.setText("Count: " + mCount);
          editor = settings.edit(); 
          editor.putInt("mCount", mCount);
          editor.commit();
      }
  });

  countButton2 = (Button) findViewById(R.id.ButtonCount2);

  countButton2.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
          //do something here
      }
  });

  countButton3 = (Button) findViewById(R.id.ButtonCount3);

  countButton3.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
          //do something else here
      }
  });

【讨论】:

  • 这就是你的本意吗?
  • 这只是示例代码。您必须将注释部分替换为您想要实际执行的操作。
猜你喜欢
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 1970-01-01
相关资源
最近更新 更多