数组按钮在文本视图中显示相同的文本,如何更改?

时间:2016-01-12 13:45:27

标签: android arrays button textview

以下应用代码由按行排列为五行且总共六行的按钮组成。 一行中有五个按钮,即一个,两个,三个,四个,五个,通过数组idArray初始化。

现在,如果我按下按钮一,文字"一个"应该是可见的,如果我按下按钮两个文本"一个"应改为"两个"

但是在我的代码中,所有按下的五个按钮都显示为#34;一个"仅

请仔细阅读我的代码并更正我,我哪里错了?

继承代码

package com.example.numbers;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class NumbersActivity extends Activity{

private static final int[] idArray = {R.id.bt0,R.id.bt1,R.id.bt2,R.id.bt3,R.id.bt4};
private Button[] bt = new Button[idArray.length];

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.numbers);

    TextView one = (TextView) findViewById(R.id.one);
    one.setVisibility(View.GONE);

    for (int i=0;i<idArray.length;i++){
        final int b = i;
        bt[b]= (Button)findViewById(idArray[b]); //Fetch the view id from array

    bt[b].setOnTouchListener(new View.OnTouchListener() { 
        TextView one = (TextView) findViewById(R.id.one);


        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()) {

    case R.id.bt0:
                one.setText(getString(R.string.one));

            case R.id.bt1:
                one.setText(getString(R.string.two));

            case R.id.bt2:
                one.setText(getString(R.string.three));

            case R.id.bt3:
                one.setText(getString(R.string.four));

            case R.id.bt4:
                one.setText(getString(R.string.five));


                case MotionEvent.ACTION_DOWN:
                    // PRESSED
                    one.setVisibility(View.VISIBLE);
                  return true; // if you want to handle the touch event

                case MotionEvent.ACTION_UP:
                    // RELEASED
                    one.setVisibility(View.INVISIBLE);
                  return true; // if you want to handle the touch event

            }
            return false;
        }
    });
  }

}

}

当我按bt1文本显示为一个,但当我按bt2文本显示为&#34;一个&#34;只是,它应该显示&#34;两个&#34;代替。 请帮我处理我的代码并告诉我哪里出错了。 在此先感谢

3 个答案:

答案 0 :(得分:2)

问题在于你的for循环中的变量i初始化为final,而final不会改变,所以直接删除最终版本或你i

for (int i=0;i<idArray.length;i++){
    int b = i;
    bt[b]= (Button)findViewById(idArray[b]); //Fetch the view id from array
  

在Java编程语言中,final关键字在几个不同的上下文中用于定义一个只能分配一次的实体。

     

一旦指定了最终变量,它总是包含相同的值

答案 1 :(得分:1)

在每个案例之后添加break,如下所示:

case R.id.bt1:
    one.setText(getString(R.string.two));
    break;

答案 2 :(得分:0)

在适当尊重上述评论和@Context,@ user3676184,@ njzk2指出的愚蠢错误的情况下,我在我的代码中进行了适当的更改以获得我想要的内容。 这是下面的工作代码:

package com.example.numbers;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class NumbersActivity extends Activity{

private static final int[] idArray = {R.id.bt0,R.id.bt1,R.id.bt2,R.id.bt3,R.id.bt4};
private Button[] bt = new Button[idArray.length];

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.numbers);

    TextView one = (TextView) findViewById(R.id.one);
    one.setVisibility(View.GONE);

    for (int i=0;i<idArray.length;i++){
        final int b = i;
        bt[b]= (Button)findViewById(idArray[b]); //Fetch the view id from array

    bt[b].setOnTouchListener(new View.OnTouchListener() { 
        TextView one = (TextView) findViewById(R.id.one);


        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(v.getId()) {

        case R.id.bt0:
                one.setText(getString(R.string.one));
                    break;
            case R.id.bt1:
                one.setText(getString(R.string.two));
                    break;
            case R.id.bt2:
                one.setText(getString(R.string.three));
                    break;
            case R.id.bt3:
                one.setText(getString(R.string.four));
                    break;  
            case R.id.bt4:
                one.setText(getString(R.string.five));
                    break;
            }
            switch(event.getAction()) {

                case MotionEvent.ACTION_DOWN:
                    // PRESSED
                    one.setVisibility(View.VISIBLE);
                  return true; // if you want to handle the touch event

                case MotionEvent.ACTION_UP:
                    // RELEASED
                    one.setVisibility(View.INVISIBLE);
                  return true; // if you want to handle the touch event
                  }  
            }
            return false;
        }
    });
  }

}

}

我所做的只是对switch案例进行了排序,将switch(event.getAction())替换为switch(v.getId())以取消按钮的案例陈述,并将switch(event.getAction())添加到MotionEvent.ACTION_DOWN的案例陈述中MotionEvent.ACTION_UP是的,我也从final

中删除了int i

现在,我的应用根据相应的按键触摸显示文字。这就是我真正想要的。

相关问题