显示按钮点击数组(锻炼应用程序的设置和代表)

时间:2015-06-26 02:29:16

标签: java android

我正在尝试创建一个跟踪练习的集合和代表的锻炼应用程序的模拟。

以下是布局的外观:

layout

它的工作原理是当有人按下NEW时会创建一个新的SET。 SET激活后,按TAP键执行您所执行的代表数量。要结束当前SET,再次按NEW并开始新的SET。要完成所有设置,请按END。当您点击一堆REP后按NEW时,它会用数字更新TextView(测试)。这是我正在努力做的基本要点。

我正在尝试将数据保存在长度为5的整数数组中。数组的索引是SET编号,索引处的值是REP的数量。

示例:我按NEW(数组索引为1),I TAP 5次(索引1 = 5的值),按NEW(索引2处的数组),I TAP 6次(索引2处的值= 6)等等...

所以我的最终数组值将是5,6,5,3,5。我将在TextView上显示它。

以下是我的Workout.java中的代码:

 public class Workout extends ActionBarActivity {

    private Button newSet;
    private Button tap;
    private Button end;
    private Integer[] score;
    int clickCount = 0;
    int tapCount = 0;
    boolean endClicked = false;
    TextView displayTest;

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

        score = new Integer[5];

        newSet = (Button)findViewById(R.id.newSetButton);

        displayTest = (TextView)findViewById(R.id.displayScore);


        newSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickCount+=1;
                if (clickCount==1) {
                    score[clickCount]= timesTapped();//replace with number of times tap is clicked
                    displayTest.append(String.valueOf(score[clickCount])); //update view
                }
                else if (clickCount==2) {
                    score[clickCount]= timesTapped(); //replace with number of times tap is clicked
                    displayTest.append(String.valueOf(score[clickCount])); //update view
                }
                else if (clickCount==3) {
                    score[clickCount]= timesTapped(); //replace with number of times tap is clicked
                    displayTest.append(String.valueOf(score[clickCount])); //update view
                }
                else if (clickCount==4) {
                    score[clickCount]= timesTapped(); //replace with number of times tap is clicked
                    displayTest.append(String.valueOf(score[clickCount])); //update view
                }
                else if (clickCount==5) {
                    score[clickCount]= timesTapped(); //replace with number of times tap is clicked
                    displayTest.append(String.valueOf(score[clickCount])); //update view
                }
            }
        });

    }

    //tap button counter method
    public int timesTapped() {

        tap =(Button)findViewById(R.id.tapButton);
        end =(Button)findViewById(R.id.endWorkoutButton);

        tap.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                tapCount+=1; //update tap count
            }
        });


        newSet.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                endClicked = true; //if button is clicked it will display
            }
        });

        if (endClicked == true) {
            return tapCount; //amount to display
        }
        else return 0;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_workout, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

它在我的模拟器上运行没有任何错误,但它没有做我认为应该做的事情。如何使实例工作?

如果我想拥有一个SET,那么每个SET都有五个REPS值。我将如何实现这一目标?我是否要上新课,然后从那里开始学习?如果是这样,怎么样?

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

为什么你把它复杂化我的朋友!根据我的理解,您只需将Tab按钮上的点击次数与包含新按钮点击次数的数组索引进行签名即可!但是代码根本没有与此相关。

实际上它非常简单,你只需要2个计数器,一个为你Tab键,另一个为新的,当用户点击新增加1然后当用户点击tab时,它会将值添加到索引并设置标志为true,所以在点击另一个新的textView后会更新。

    int newIndex=0;
    int tabCount=0;
    int[] score=new Integer[5];
    String result;
    boolean flag=false;
    newSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            if(flag){
            result+=" "+tabCount+"at"+newIndex+" ";
                textView.setText(result);}
             newIndex+=1;
            }});
    tab.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                tabCount+=1;
                score[newIndex]=tabCount;
                flag=true;
            }
        });

注意:该标志是为了确保用户完成了他想要的标签数量,但是您可以更新标签textView中的onClick()但是使textView内容不准确