0

我想将文本存储在变量中的按钮上。

我参考了这篇文章,但它定义了“一个按钮一个变量”。我的应用程序将有大约 80-90 个按钮。如何将来自这些按钮中的任何一个的数据存储在单个变量中,并使用该公共变量进行进一步操作?

例子 -

我有按钮 - 1,2,3,4,5,6,7,8,9

我如何将它们初始化为btn = (Button) findViewByIdjava中的单个按钮(...事物),从而将它们的文本放在一个变量中?因为我不能在 java 中分别定义 90 个按钮!

我怎样才能做到这一点?

这是我所做的代码..(一种完全愚蠢或低效的方式)-

它的基本作用是 - 在 12 个按钮中,无论哪个被按下,都将它与一个字符串连接起来。就像如果按下 1 字符串将是 01 之后如果按下 10 字符串将变为 0110 等等......

public class FEa extends Activity implements View.OnClickListener
{
    Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12;
    TextView tvDisp;
    String acc;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fea);
        initialise();
    }

    private void initialise()
    {
        b1 = (Button) findViewById (R.id.Btn1);
        b1.setOnClickListener(this);
        b2 = (Button) findViewById (R.id.Btn2);
        b2.setOnClickListener(this);
        b3 = (Button) findViewById (R.id.Btn3);
        b3.setOnClickListener(this);
        b4 = (Button) findViewById (R.id.Btn4);
        b4.setOnClickListener(this);
        b5 = (Button) findViewById (R.id.Btn5);
        b5.setOnClickListener(this);
        b6 = (Button) findViewById (R.id.Btn6);
        b6.setOnClickListener(this);
        b7 = (Button) findViewById (R.id.Btn7);
        b7.setOnClickListener(this);
        b8 = (Button) findViewById (R.id.Btn8);
        b8.setOnClickListener(this);
        b9 = (Button) findViewById (R.id.Btn9);
        b9.setOnClickListener(this);
        b10 = (Button) findViewById (R.id.Btn10);
        b10.setOnClickListener(this);
        b11 = (Button) findViewById (R.id.Btn11);
        b11.setOnClickListener(this);
        b12 = (Button) findViewById (R.id.Btn12);
        b12.setOnClickListener(this);
        tvDisp=(TextView) findViewById (R.id.TxtViewDisplay);

    }

    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub

        switch(v.getId())
        {
        case R.id.Btn1:

            if(acc == null)
            {
                acc = "01";
                tvDisp.setText("String Is:" + acc);
            }
            else
            {
                acc=acc + "0"+1;
                tvDisp.setText("String Is:" + acc);
            }

            break;
        case R.id.Btn2:

            if(acc == null)
            {
                acc = "02";
                tvDisp.setText("String Is:" + acc);     
            }
            else
            {
                acc = acc + "0"+2;
                tvDisp.setText("String Is:" + acc);
            }
            break;
        case R.id.Btn3:

            if(acc == null)
            {
                acc = "03";
                tvDisp.setText("String Is:" + acc);     
            }
            else
            {
                acc = acc + "0"+3;
                tvDisp.setText("String Is:" + acc);
            }
            break;
        case R.id.Btn4:

            if(acc == null)
            {
                acc = "04";
                tvDisp.setText("String Is:" + acc);     
            }
            else
            {
                acc = acc + "0"+4;
                tvDisp.setText("String Is:" + acc);
            }
            break;
        case R.id.Btn5:

            if(acc == null)
            {
                acc = "05";
                tvDisp.setText("String Is:" + acc);     
            }
            else
            {
                acc = acc + "0"+5;
                tvDisp.setText("String Is:" + acc);
            }
            break;
        case R.id.Btn6:

            if(acc == null)
            {
                acc = "06";
                tvDisp.setText("String Is:" + acc);     
            }
            else
            {
                acc = acc + "0"+6;
                tvDisp.setText("String Is:" + acc);
            }
            break;
        case R.id.Btn7:

            if(acc == null)
            {
                acc = "07";
                tvDisp.setText("String Is:" + acc);     
            }
            else
            {
                acc = acc + "0"+7;
                tvDisp.setText("String Is:" + acc);
            }
            break;
        case R.id.Btn8:

            if(acc == null)
            {
                acc = "08";
                tvDisp.setText("String Is:" + acc);     
            }
            else
            {
                acc = acc + "0"+8;
                tvDisp.setText("String Is:" + acc);
            }
            break;
        case R.id.Btn9:

            if(acc == null)
            {
                acc = "09";
                tvDisp.setText("String Is:" + acc);     
            }
            else
            {
                acc = acc + "0" +9;
                tvDisp.setText("String Is:" + acc);
            }
            break;
        case R.id.Btn10:

            if(acc == null)
            {
                acc = "10";
                tvDisp.setText("String Is:" + acc);     
            }
            else
            {
                acc = acc + 10;
                tvDisp.setText("String Is:" + acc);
            }
            break;
        case R.id.Btn11:

            if(acc == null)
            {
                acc = "11";
                tvDisp.setText("String Is:" + acc);     
            }
            else
            {
                acc = acc + 11;
                tvDisp.setText("String Is:" + acc);
            }
            break;
        case R.id.Btn12:

            if(acc == null)
            {
                acc = "12";
                tvDisp.setText("String Is:" + acc);     
            }
            else
            {
                acc = acc + 12;
                tvDisp.setText("String Is:" + acc);
            }
            break;
        }
    }
}
4

3 回答 3

3

我认为您应该让每个按钮都调用相同的 onClick 方法,您可以在 xml 中为所有按钮设置该方法:

<Button android:layout_height="45dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onButtonClick" />

然后在你的活动中,你可以很容易地积累你的字符串:

public class FEa extends Activity
{
   TextView tvDisp;
   String acc = "";

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.fea);
       tvDisp = (TextView) findViewById (R.id.TxtViewDisplay);
   }

    public void onButtonClick(View v){
        Button theButton = (Button)v;
        acc = acc + theButton.getText().toString();
        tvDisp.setText("String Is: " + acc);
    }

}
于 2012-12-07T20:49:15.647 回答
0

我认为您可以使用某些视图(例如视图组)来包含您的按钮。然后你只需要在 for() 或 while() 中使用 getChildAt(int index) 来获取任何列表中的每个按钮并一个接一个地从中获取文本。

我从未尝试过,但我认为我可以工作。

希望它有所帮助。

于 2012-12-07T18:32:26.030 回答
0

我尚未测试此代码,但您可以将所有按钮(在 xml 中)的标签设置为“按钮”,然后尝试将所有文​​本存储在 a 中HashMap

HashMap<Integer, String> buttonTexts;
RelativeLayout mainLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.fea);
    buttonTexts = new HashMap<Integer, String>();
    mainLayout = (RelativeLayout)findViewById(R.id.myRelativeLayout);
    getTxtFromBtns();
    Log.i("HashMapValues", buttonTexts.toString());
}

public void getTxtFromBtns() {
    for (int i = 0; i <= mainLayout.getChildCount(); i++) {
        View view = mainLayout.getChildAt(i);
        String buttonChk = (String) view.getTag();
        if (buttonChk.equals("button")) {
            Button newBtn = (Button) view;
            String buttonText = (String) newBtn.getText();
            buttonTexts.put(view.getId(), buttonText);
        }
    }
}

然后你可以简单地检索文本

String myButton = buttonTexts.get(R.id.Btn1);
于 2012-12-07T18:39:13.723 回答