我想将文本存储在变量中的按钮上。
我参考了这篇文章,但它定义了“一个按钮一个变量”。我的应用程序将有大约 80-90 个按钮。如何将来自这些按钮中的任何一个的数据存储在单个变量中,并使用该公共变量进行进一步操作?
例子 -
我有按钮 - 1,2,3,4,5,6,7,8,9
我如何将它们初始化为btn = (Button) findViewById
java中的单个按钮(...事物),从而将它们的文本放在一个变量中?因为我不能在 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;
}
}
}