How Can I Disable The Changable Textview And Click On It Reenable The Button?
Solution 1:
Sparse...Array
s are very useful because they can act as a kind of Map
while avoiding autoboxing if you're working with primitive data types. For further information regarding available methods, please take a look at the reference.
The following code uses SparseIntArray
in two places: for Button
s, we want to map the resource id to the number of each Button
.
For TextView
s, we need to keep track of their state: are they filled, and if so, with which number? So we use a SparseIntArray
only for filled TextView
s. The indices from textView are the keys and the numbers are the values.
The Activity
will handle clicks on Button
s as well as TextView
s. The values array is no longer necessary, the same goes for size.
So here's the entire Activity
code - enjoy :)
privatestaticfinalStringEMPTY=" ";
private TextView textView[] = newTextView[4];
private SparseIntArray mapOfButtons, // map id to number
mapOfFilledTextViews; // map array index to value if TextView is filled@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a8__button_on_touch_and_on_click);
initMaps();
textView[0] = (TextView) findViewById(R.id.text_1);
textView[1] = (TextView) findViewById(R.id.text_2);
textView[2] = (TextView) findViewById(R.id.text_3);
textView[3] = (TextView) findViewById(R.id.text_4);
for (TextView tv: textView)
{
tv.setOnClickListener(this);
}
Buttonb1= (Button) findViewById(R.id.button_1);
Buttonb2= (Button) findViewById(R.id.button_2);
Buttonb3= (Button) findViewById(R.id.button_3);
Buttonb4= (Button) findViewById(R.id.button_4);
Buttonb5= (Button) findViewById(R.id.button_5);
Buttonb6= (Button) findViewById(R.id.button_6);
Buttonb7= (Button) findViewById(R.id.button_7);
Buttonb8= (Button) findViewById(R.id.button_8);
Buttonb9= (Button) findViewById(R.id.button_9);
Buttonb0= (Button) findViewById(R.id.button_0);
Buttonent= (Button) findViewById(R.id.button_enter);
Buttonclr= (Button) findViewById(R.id.button_clear);
buttonEffect(b0);
buttonEffect(b1);
buttonEffect(b2);
buttonEffect(b3);
buttonEffect(b4);
buttonEffect(b5);
buttonEffect(b6);
buttonEffect(b7);
buttonEffect(b8);
buttonEffect(b9);
buttonEffect(clr);
buttonEffect(ent);
}
@OverridepublicvoidonClick(View v)
{
intid= v.getId();
if (v instanceof Button)
{
if (mapOfButtons.indexOfKey(id) < 0)
{ // so this is no number buttonif (id == R.id.button_clear)
{
if (mapOfFilledTextViews.size() == 0)
{
return;
}
// get the key = the index for 'textView'intindexToClear= indexOfRightmostFilledTextView();
// get the valueintnumberToClear= mapOfFilledTextViews.get(indexToClear);
clearButton(numberToClear);
clearTextView(indexToClear);
bindText();
}
elseif (id == R.id.button_enter)
{
// do something
}
}
else// this is a number button
{
// only accept clicks if there is a free TextViewif (mapOfFilledTextViews.size() == textView.length)
{
Toast.makeText(this,
"First clear one of the selected numbers", Toast.LENGTH_SHORT).show();
return;
}
Buttonb= (Button)v;
b.setBackgroundResource(R.drawable.redww);
b.setEnabled(false);
intnumber= mapOfButtons.get(id);
setText(number);
}
}
elseif (v instanceof TextView)
{
TextViewtv= (TextView)v;
intnumber= Integer.parseInt(tv.getText().toString() );
// 'indexOfValue()' will work because we don't have duplicate valuesinti= mapOfFilledTextViews.indexOfValue(number);
intkey= mapOfFilledTextViews.keyAt(i);
clearButton(number);
clearTextView(key);
bindText();
}
}
privatestaticvoidbuttonEffect(View button)
{
button.setOnTouchListener(newView.OnTouchListener()
{
publicbooleanonTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
v.getBackground().setColorFilter(0xe0ffffff, PorterDuff.Mode.SRC_ATOP);
v.invalidate();
break;
}
case MotionEvent.ACTION_UP:
{
v.getBackground().clearColorFilter();
v.invalidate();
break;
}
}
returnfalse;
}
});
}
privatevoidinitMaps()
{
// empty in the beginning:
mapOfFilledTextViews = newSparseIntArray(4);
mapOfButtons = newSparseIntArray(10);
mapOfButtons.put(R.id.button_0, 0);
mapOfButtons.put(R.id.button_1, 1);
mapOfButtons.put(R.id.button_2, 2);
mapOfButtons.put(R.id.button_3, 3);
mapOfButtons.put(R.id.button_4, 4);
mapOfButtons.put(R.id.button_5, 5);
mapOfButtons.put(R.id.button_6, 6);
mapOfButtons.put(R.id.button_7, 7);
mapOfButtons.put(R.id.button_8, 8);
mapOfButtons.put(R.id.button_9, 9);
}
privatevoidbindText()
{
int value;
intlen= textView.length;
for (inti=0; i < len; i++)
{
value = mapOfFilledTextViews.get(i, -1);
textView[i].setText(value >= 0 ? String.valueOf(value) : EMPTY);
}
}
privatevoidsetText(int number)
{
if (mapOfFilledTextViews.size() >= textView.length)
{
// this should not happen if we check the number// of free TextViews in onClick()return;
}
intindex= indexOfLowestFreeTextView();
mapOfFilledTextViews.put(index, number);
bindText();
}
privatevoidclearButton(int number)
{
// get the first (in our case only) index with valueAt(index) == numberintindex= mapOfButtons.indexOfValue(number);
if (index < 0) // this value does not exist
{
// this should not happen!// so maybe throw a RuntimeExceptionreturn;
}
intid= mapOfButtons.keyAt(index);
Buttonb= (Button) findViewById(id);
b.setBackgroundResource(R.drawable.greenww);
b.setEnabled(true);
}
privatevoidclearTextView(int index)
{
// remove entry as this TextView is no longer filled
mapOfFilledTextViews.delete(index);
}
/**
*
* @return index (for use with 'textView') if there are filled TextViews, else -1
*/privateintindexOfRightmostFilledTextView()
{
intsize= mapOfFilledTextViews.size();
intmaxKey= -1;
for (inti=0; i < size; i++)
{
intiTemp= mapOfFilledTextViews.keyAt(i);
if (iTemp > maxKey )
{
maxKey = iTemp;
}
}
return maxKey;
}
/**
* Get index of leftmost (lowest) free TextView
* @return index for use with 'textView'.
* If all TextViews are filled, return textView.length
*/privateintindexOfLowestFreeTextView()
{
intlowestIndex= textView.length;
// this assumes the TextViews are sorted from left to right in the textView arrayfor (intkey=0; key < textView.length; key++)
{
if (mapOfFilledTextViews.indexOfKey(key) < 0)
{
lowestIndex = key;
break;
}
}
return lowestIndex;
}
Post a Comment for "How Can I Disable The Changable Textview And Click On It Reenable The Button?"