How Can I Put Numbers In Several Textviews?
Hey I have gridview buttons like keyboard with green light if I click on one of them turns to red anyways and I have 4 TextViews so I want when I press a number text it on textview
Solution 1:
int click = 0;
TextView tv [] = new TextView[4];
onCreate:
tv[0] = (TextView) findViewById(R.id.tv1);
tv[1] = (TextView) findViewById(R.id.tv2);
tv[2] = (TextView) findViewById(R.id.tv3);
tv[3] = (TextView) findViewById(R.id.tv4);
onClick:
if(click <= 3) {
switch (v.getId()){
case R.id.n1:
number = "1";
clickCount.add(0);
btn[0].setEnabled(false);
break;
}
setTv(number, click);
click++;
}
metod:
publicvoidsetTv(String str, int click){
switch (click){
case0:
tv[0].setText(str);
break;
case1:
tv[1].setText(str);
break;
case2:
tv[2].setText(str);
break;
case3:
tv[3].setText(str);
break;
}
}
delete button in:
if(click != 0) {
tv[--click].setText("");
}
xml file:
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="400dp"android:gravity="bottom"android:orientation="vertical"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:focusable="true"android:focusableInTouchMode="true"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="3dp"android:orientation="horizontal"><Buttonandroid:id="@+id/n1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:minHeight="80dp"android:text="1"android:textColor="#333"android:textSize="25sp" /><Buttonandroid:id="@+id/n2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="2dp"android:layout_weight="1"android:minHeight="80dp"android:text="2"android:textColor="#333"android:textSize="25sp" /><Buttonandroid:id="@+id/n3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="2dp"android:layout_marginRight="2dp"android:layout_weight="1"android:minHeight="80dp"android:text="3"android:textColor="#333"android:textSize="25sp" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="#112"android:minHeight="80dp"android:text="clear"android:textColor="@color/colorPrimaryDark"android:textSize="25sp"android:textStyle="bold"android:id="@+id/Clear" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="2dp"android:orientation="horizontal"><Buttonandroid:id="@+id/n4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:minHeight="80dp"android:text="4"android:textColor="#333"android:textSize="25sp" /><Buttonandroid:id="@+id/n5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="2dp"android:layout_weight="1"android:minHeight="80dp"android:text="5"android:textColor="#333"android:textSize="25sp" /><Buttonandroid:id="@+id/n6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="2dp"android:layout_marginRight="2dp"android:layout_weight="1"android:minHeight="80dp"android:text="6"android:textColor="#333"android:textSize="25sp" /><Buttonandroid:id="@+id/enter"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="#ccc"android:minHeight="80dp"android:text="enter"android:textColor="@color/colorPrimaryDark"android:textSize="25sp"android:textStyle="bold" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="2dp"android:orientation="horizontal"><Buttonandroid:id="@+id/n7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:minHeight="80dp"android:text="7"android:textColor="#333"android:textSize="25sp" /><Buttonandroid:id="@+id/n8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="2dp"android:layout_weight="1"android:minHeight="80dp"android:text="8"android:textColor="#333"android:textSize="25sp" /><Buttonandroid:id="@+id/n9"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="2dp"android:layout_marginRight="2dp"android:layout_weight="1"android:minHeight="80dp"android:text="9"android:textColor="#333"android:textSize="25sp" /><Buttonandroid:id="@+id/n0"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="2dp"android:layout_weight="1"android:minHeight="80dp"android:text="0"android:textColor="#333"android:textSize="25sp" /></LinearLayout></LinearLayout><LinearLayoutandroid:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:weightSum="4"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"><TextViewandroid:id="@+id/tv1"android:layout_width="0dp"android:layout_height="60dp"android:gravity="center"android:background="@drawable/tvstroke"android:layout_weight="1"/><TextViewandroid:id="@+id/tv2"android:layout_width="0dp"android:layout_height="60dp"android:gravity="center"android:background="@drawable/tvstroke"android:layout_weight="1"android:layout_marginRight="5dp"android:layout_marginLeft="5dp"/><TextViewandroid:id="@+id/tv3"android:layout_width="0dp"android:layout_height="60dp"android:background="@drawable/tvstroke"android:gravity="center"android:layout_weight="1"android:layout_marginRight="5dp"/><TextViewandroid:id="@+id/tv4"android:layout_width="0dp"android:background="@drawable/tvstroke"android:layout_height="60dp"android:gravity="center"android:layout_weight="1"/></LinearLayout></RelativeLayout>
TextView stroke:
<?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"><strokeandroid:width="1dp"android:color="@color/colorPrimary"/></shape>
Main Activity:
publicclassMainActivityextendsAppCompatActivityimplementsView.OnClickListener{
int click = 0;
TextView tvKeyBoard;
TextView tv [] = newTextView[4];
Button btn[] = newButton[10];
ArrayList<Integer> clickCount = newArrayList<>();
String fullNamber = "";
Button del, clear;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
del = (Button) findViewById(R.id.enter);
clear = (Button) findViewById(R.id.Clear);
tv[0] = (TextView) findViewById(R.id.tv1);
tv[1] = (TextView) findViewById(R.id.tv2);
tv[2] = (TextView) findViewById(R.id.tv3);
tv[3] = (TextView) findViewById(R.id.tv4);
btn[0] = (Button) findViewById(R.id.n1);
btn[1] = (Button) findViewById(R.id.n2);
btn[2] = (Button) findViewById(R.id.n3);
btn[3] = (Button) findViewById(R.id.n4);
btn[4] = (Button) findViewById(R.id.n5);
btn[5] = (Button) findViewById(R.id.n6);
btn[6] = (Button) findViewById(R.id.n7);
btn[7] = (Button) findViewById(R.id.n8);
btn[8] = (Button) findViewById(R.id.n9);
btn[9] = (Button) findViewById(R.id.n0);
for (Button b : btn){
b.setOnClickListener(this);
}
del.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
if(clickCount.size() != 0) {
btn[clickCount.get(clickCount.size()-1)].setEnabled(true);
clickCount.remove(clickCount.size()-1);
}
if(click != 0) {
tv[--click].setText("");
}
}
});
clear.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
for (int i = 0; i < clickCount.size(); i++ ){
btn[clickCount.get(i)].setEnabled(true);
}
clickCount.clear();
}
});
}
@OverridepublicvoidonClick(View v) {
Stringnumber = "";
if(click <= 3) {
switch (v.getId()){
case R.id.n1:
number = "1";
clickCount.add(0);
btn[0].setEnabled(false);
break;
case R.id.n2:
number = "2";
clickCount.add(1);
btn[1].setEnabled(false);
break;
case R.id.n3:
number = "3";
clickCount.add(2);
btn[2].setEnabled(false);
break;
case R.id.n4:
number = "4";
clickCount.add(3);
btn[3].setEnabled(false);
break;
case R.id.n5:
number = "5";
clickCount.add(4);
btn[4].setEnabled(false);
break;
case R.id.n6:
number = "6";
clickCount.add(5);
btn[5].setEnabled(false);
break;
case R.id.n7:
number = "7";
clickCount.add(6);
btn[6].setEnabled(false);
break;
case R.id.n8:
number = "8";
clickCount.add(7);
btn[7].setEnabled(false);
break;
case R.id.n9:
number = "9";
clickCount.add(8);
btn[8].setEnabled(false);
break;
case R.id.n0:
number = "0";
clickCount.add(9);
btn[9].setEnabled(false);
break;
}
setTv(number, click);
click++;
}
}
@OverridepublicvoidonBackPressed() {
if(del.isEnabled()){
for (Button b : btn){
b.setVisibility(View.INVISIBLE);
}
del.setVisibility(View.INVISIBLE);
clear.setVisibility(View.INVISIBLE);
}elsesuper.onBackPressed();
}
publicvoidsetTv(String str, int count){
switch (count){
case0:
tv[0].setText(str);
break;
case1:
tv[1].setText(str);
break;
case2:
tv[2].setText(str);
break;
case3:
tv[3].setText(str);
break;
}
}
}
Solution 2:
For brevity, I've only chosen five buttons.
MainActivity
publicclassMainActivityextendsActivity {
List<String> values = newArrayList<>();
String[] values = newString[]{" ", " ", " ", " "};
TextView textView[] = newTextView[4];
int size = 0;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
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);
// bind your buttons views here// for ex:
button0 = (Button) findViewById(R.id.button_0);
// so on up to button 9// ...//Also bind views for Clear button and enter
}
// This method is set statically in xml file check belowpublicvoidonClick(View v) {
switch (v.getId()) {
case R.id.button_0:
setText(0);
button0.setEnabled(false);
break;
case R.id.button_1:
setText(1);
button1.setEnabled(false);
break;
case R.id.button_2:
setText(2);
button2.setEnabled(false);
break;
case R.id.button_3:
setText(3);
button3.setEnabled(false);
break;
case R.id.button_4:
setText(4);
button4.setEnabled(false);
break;
case R.id.button_clear:
if (size == 0) {
return;
}
String numberToCleared = values[size - 1];
clearButton(Integer.parseInt(numberToCleared));
size--;
values[size] = " ";
bindText();
break;
case R.id.button_enter:
//do what you want to do herebreak;
}
}
privatevoidclearButton(int number) {
switch (number) {
case0:
button0.setEnabled(true);
break;
case1:
button1.setEnabled(true);
break;
case2:
button2.setEnabled(true);
break;
case3:
button3.setEnabled(true);
break;
case4:
button4.setEnabled(true);
break;
}
}
publicvoidbindText() {
for (int i = 0; i < 4; i++) {
textView[i].setText(values[i]);
}
}
publicvoidsetText(int number) {
if (size == 4) {
clearButton(Integer.parseInt(values[3]));
size--;
}
values[size] = String.valueOf(number);
size++;
bindText();
}
}
layout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:orientation="horizontal"><TextViewandroid:id="@+id/text_1"android:layout_width="24dp"android:layout_height="wrap_content"android:padding="4dp"/><TextViewandroid:id="@+id/text_2"android:layout_width="24dp"android:layout_height="wrap_content"android:padding="4dp"/><TextViewandroid:id="@+id/text_3"android:layout_width="24dp"android:layout_height="wrap_content"android:padding="4dp"/><TextViewandroid:id="@+id/text_4"android:layout_width="24dp"android:layout_height="wrap_content"android:padding="4dp"/></LinearLayout><GridLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:columnCount="3"android:orientation="vertical"android:rowCount="2"><Buttonandroid:id="@+id/button_0"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/button_selector"android:onClick="onClick"android:text="0"/><Buttonandroid:id="@+id/button_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/button_selector"android:onClick="onClick"android:text="1"/><Buttonandroid:id="@+id/button_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/button_selector"android:onClick="onClick"android:text="2"/><Buttonandroid:id="@+id/button_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/button_selector"android:onClick="onClick"android:text="3"/><Buttonandroid:id="@+id/button_4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/button_selector"android:onClick="onClick"android:text="4"/><Buttonandroid:id="@+id/button_clear"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/button_selector"android:onClick="onClick"android:text="Clear"/><Buttonandroid:id="@+id/button_enter"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/button_selector"android:onClick="onClick"android:text="Enter"/></GridLayout></LinearLayout>
button_selector.xml Place this in your drawable folder.
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@color/green"android:state_enabled="true"/><itemandroid:drawable="@color/red"android:state_enabled="false"/><itemandroid:drawable="@color/green"/></selector>
Logic (as OP asked for it)
The values
array stores the numbers that are supposed to be displayed in TextViews.
When a button is pressed the values array is updated in the setText() method.
When clear is pressed this is what happens -
if (size == 0) {
return; // if size is 0 do nothing.
}
String numberToCleared = values[size - 1];
//You've to clear the button color. So, get the last value in array//and send it to clearButton() function.clearButton(Integer.parseInt(numberToCleared));
//remove the number from values array and decrement size
size--;
values[size] = " ";
bindText();
break;
Post a Comment for "How Can I Put Numbers In Several Textviews?"