Reputation: 117
I want to store the text on a button in a variable.
I refered This article but it defines for "One variable for one button".My app is going to have around 80-90 buttons. How can I store data from any of these buttons in a single variable and use that common variable for further operations?
Example -
I have buttons - 1,2,3,4,5,6,7,8,9
How can I initialise them to a single button (the btn = (Button) findViewById
... thing) in java and thus get their text in one single variable? Coz i cant define 90 buttons separately in java!!
How can i do this?
Here is the code of what ive done..(A completely supid or inefficient way)-
What it basically does is - out of 12 buttons,whichever is pressed,concatinates it with a string. like if 1 is pressed the string will be 01 after that if 10 is pressed, the string will become 0110 and so on...
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;
}
}
}
Upvotes: 0
Views: 1775
Reputation: 5876
I think you should have every button call the same onClick method, which you can set in xml for all buttons:
<Button android:layout_height="45dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClick" />
Then in your activity, you can accumulate your string very easily:
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);
}
}
Upvotes: 3
Reputation: 8925
I haven't tested this code but you could set the tags for all of the buttons (in xml) to "button" then try something like storing all of the text in 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);
}
}
}
Then you could retrieve the text simply by
String myButton = buttonTexts.get(R.id.Btn1);
Upvotes: 0
Reputation: 3511
I think that you could use some view like a view group to contain your buttons. Then you just have to use getChildAt(int index) in a for() or while() to get each button in any list and get the text from them one after another.
I never tried it but i think that i could work.
Hopes it helps.
Upvotes: 0