[英]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 textview1 then second number goes to txt2 the third to txt3 the fourth to txt4 but the txt4 must be changeable if I pressed on another number automatically changes and it is important that I can't use same number for example in txt1 I pressed number 1 the button goes red so I can't use it in txt2,3,4 Until I delete it from txt1 look at my picture and if someone want the code i'm happy to put it嘿,我有 gridview 按钮,比如带绿灯的键盘,如果我点击其中一个会变成红色,我有 4 个 TextView,所以我想要当我在 textview1 上按下一个数字文本时,第二个数字转到 txt2 第三个到 txt3 第四个到 txt4 但 txt4 必须是可变的,如果我按下另一个数字会自动更改,重要的是我不能使用相同的数字,例如在 txt1 我按下数字 1 按钮变为红色,所以我不能在 txt2 中使用它, 3,4 直到我从 txt1 中删除它,看看我的图片,如果有人想要代码,我很乐意把它
(and how automatically jumps to the next textview after one number) (以及如何在一个数字后自动跳转到下一个文本视图)
MainActivity.java this helps me with edit text I want it for TextView MainActivity.java 这有助于我编辑文本我想要它用于 TextView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1 = (TextView)findViewById(R.id.txt1);
editText = (EditText) findViewById(R.id.editText);
b1 = (Button) findViewById(R.id.n1);
b2 = (Button) findViewById(R.id.n2);
b3 = (Button) findViewById(R.id.n3);
b4 = (Button) findViewById(R.id.n4);
b5 = (Button) findViewById(R.id.n5);
b6 = (Button) findViewById(R.id.n6);
b7 = (Button) findViewById(R.id.n7);
b8 = (Button) findViewById(R.id.n8);
b9 = (Button) findViewById(R.id.n9);
b0 = (Button) findViewById(R.id.n0);
ent = (Button) findViewById(R.id.enter);
clr = (Button) findViewById(R.id.Clear);
buttonEffect(b0);
buttonEffect(b1);
buttonEffect(b2);
buttonEffect(b3);
buttonEffect(b4);
buttonEffect(b5);
buttonEffect(b6);
buttonEffect(b7);
buttonEffect(b8);
buttonEffect(b9);
/* b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
b6.setVisibility(View.INVISIBLE);
b7.setVisibility(View.INVISIBLE);
b8.setVisibility(View.INVISIBLE);
b9.setVisibility(View.INVISIBLE);
b0.setVisibility(View.INVISIBLE);
ent.setVisibility(View.INVISIBLE);
clr.setVisibility(View.INVISIBLE);*/
//for default keyboard don't appear
editText.setShowSoftInputOnFocus(false);
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
b1.setVisibility(View.VISIBLE);
b2.setVisibility(View.VISIBLE);
b3.setVisibility(View.VISIBLE);
b4.setVisibility(View.VISIBLE);
b5.setVisibility(View.VISIBLE);
b6.setVisibility(View.VISIBLE);
b7.setVisibility(View.VISIBLE);
b8.setVisibility(View.VISIBLE);
b9.setVisibility(View.VISIBLE);
b0.setVisibility(View.VISIBLE);
ent.setVisibility(View.VISIBLE);
clr.setVisibility(View.VISIBLE);
return false;
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
textContainer = s.toString();
prevLength = s.length();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
editText.setSelection(editText.getText().length());
}
@Override
public void afterTextChanged(Editable s) {
length = s.length();
if (!textContainer.isEmpty()) {
if (s.length() > 1) {
if (prevLength < length) {
if (!textContainer.contains(s.toString().subSequence(length - 1, length))) {
length = s.length();
} else {
editText.getText().delete(length - 1, length);
}
}
}
} else {
textContainer = s.toString();
}
}
});
b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "0"));
b0.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String zero = "0";
txt1.setText(zero);
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "1"));
b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String one = "1";
txt1.setText(one);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "2"));
b2.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String two = "2";
txt1.setText(two);
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "3"));
b3.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String three = "3";
txt1.setText(three);
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "4"));
b4.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String four = "4";
txt1.setText(four);
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "5"));
b5.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String five = "5";
txt1.setText(five);
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "6"));
b6.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String six = "6";
txt1.setText(six);
}
});
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "7"));
b7.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String seven = "7";
txt1.setText(seven);
}
});
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "8"));
b8.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String eight = "8";
txt1.setText(eight);
}
});
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "9"));
b9.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String nine = "9";
txt1.setText(nine);
}
});
ent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
clr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clr.setPressed(true);
int length = editText.getText().length();
if (length > 0) {
editText.getText().delete(length - 1, length);
}
if (editText.getText().toString().contains("0")) {
b0.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b0.setEnabled(false);
}
else{
b0.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b0.setEnabled(true);
}
if (editText.getText().toString().contains("1")) {
b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b1.setEnabled(false);
}
else{
b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b1.setEnabled(true);
}
if (editText.getText().toString().contains("2")) {
b2.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b2.setEnabled(false);
}
else{
b2.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b2.setEnabled(true);
}
if (editText.getText().toString().contains("3")) {
b3.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b3.setEnabled(false);
}
else{
b3.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b3.setEnabled(true);
}
if (editText.getText().toString().contains("4")) {
b4.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b4.setEnabled(false);
}
else{
b4.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b4 .setEnabled(true);
}
if (editText.getText().toString().contains("5")) {
b5.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b5.setEnabled(false);
}
else{
b5.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b5.setEnabled(true);
}
if (editText.getText().toString().contains("6")) {
b6.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b6.setEnabled(false);
}
else{
b6.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b6.setEnabled(true);
}
if (editText.getText().toString().contains("7")) {
b7.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b7.setEnabled(false);
}
else{
b7.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b7.setEnabled(true);
}
if (editText.getText().toString().contains("8")) {
b8.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b8.setEnabled(false);
}
else{
b8.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b8.setEnabled(true);
}
if (editText.getText().toString().contains("9")) {
b9.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b9.setEnabled(false);
}
else{
b9.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b9.setEnabled(true);
}
}
});
}
public static void buttonEffect(View button){
button.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(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;
}
}
return false;
}
});
}
@Override
public void onBackPressed() {
b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
b6.setVisibility(View.INVISIBLE);
b7.setVisibility(View.INVISIBLE);
b8.setVisibility(View.INVISIBLE);
b9.setVisibility(View.INVISIBLE);
b0.setVisibility(View.INVISIBLE);
ent.setVisibility(View.INVISIBLE);
clr.setVisibility(View.INVISIBLE);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
b6.setVisibility(View.INVISIBLE);
b7.setVisibility(View.INVISIBLE);
b8.setVisibility(View.INVISIBLE);
b9.setVisibility(View.INVISIBLE);
b0.setVisibility(View.INVISIBLE);
ent.setVisibility(View.INVISIBLE);
clr.setVisibility(View.INVISIBLE);
return super.onTouchEvent(event);
}
xml xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context="com.example.mike.keyboardtest.MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="350dp"
android:gravity="bottom"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="@+id/linearLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:orientation="horizontal">
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:clickable="true"
android:minHeight="80dp"
android:text="1"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:clickable="true"
android:minHeight="80dp"
android:text="2"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:clickable="true"
android:minHeight="80dp"
android:text="3"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#112"
android:minHeight="80dp"
android:text="clear"
android:clickable="true"
android:textColor="@color/colorPrimaryDark"
android:textSize="25sp"
android:textStyle="bold"
android:id="@+id/button_clear" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:clickable="true"
android:minHeight="80dp"
android:text="4"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:clickable="true"
android:minHeight="80dp"
android:text="5"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="@+id/button_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:minHeight="80dp"
android:clickable="true"
android:text="6"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="@+id/button_enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ccc"
android:minHeight="80dp"
android:clickable="true"
android:text="enter"
android:textColor="@color/colorPrimaryDark"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<Button
android:id="@+id/button_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:minHeight="80dp"
android:clickable="true"
android:text="7"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="@+id/button_8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:minHeight="80dp"
android:clickable="true"
android:text="8"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="@+id/button_9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:minHeight="80dp"
android:clickable="true"
android:text="9"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="@+id/button_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="@drawable/button_selector"
android:minHeight="80dp"
android:clickable="true"
android:text="0"
android:textColor="#333"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:id="@+id/text_1"
android:layout_width="0dp"
android:layout_height="60dp"
android:gravity="center"
android:background="@drawable/greenww"
android:layout_weight="1"/>
<TextView
android:id="@+id/text_2"
android:layout_width="0dp"
android:layout_height="60dp"
android:gravity="center"
android:background="@drawable/greenww"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"/>
<TextView
android:id="@+id/text_3"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="@drawable/greenww"
android:gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/text_4"
android:layout_width="0dp"
android:background="@drawable/greenww"
android:layout_height="60dp"
android:gravity="center"
android:layout_weight="1"/>
</LinearLayout>
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:方法:
public void setTv(String str, int click){
switch (click){
case 0:
tv[0].setText(str);
break;
case 1:
tv[1].setText(str);
break;
case 2:
tv[2].setText(str);
break;
case 3:
tv[3].setText(str);
break;
}
}
delete button in:删除按钮:
if(click != 0) {
tv[--click].setText("");
}
xml file: xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns: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">
<LinearLayout
android: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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:orientation="horizontal">
<Button
android: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" />
<Button
android: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" />
<Button
android: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" />
<Button
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<Button
android: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" />
<Button
android: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" />
<Button
android: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" />
<Button
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<Button
android: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" />
<Button
android: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" />
<Button
android: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" />
<Button
android: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>
<LinearLayout
android: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">
<TextView
android:id="@+id/tv1"
android:layout_width="0dp"
android:layout_height="60dp"
android:gravity="center"
android:background="@drawable/tvstroke"
android:layout_weight="1"/>
<TextView
android: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"/>
<TextView
android: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"/>
<TextView
android: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"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp"
android:color="@color/colorPrimary"/>
</shape>
Main Activity:主要活动:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
int click = 0;
TextView tvKeyBoard;
TextView tv [] = new TextView[4];
Button btn[] = new Button[10];
ArrayList<Integer> clickCount = new ArrayList<>();
String fullNamber = "";
Button del, clear;
@Override
protected void onCreate(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(new View.OnClickListener() {
@Override
public void onClick(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(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < clickCount.size(); i++ ){
btn[clickCount.get(i)].setEnabled(true);
}
clickCount.clear();
}
});
}
@Override
public void onClick(View v) {
String number = "";
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++;
}
}
@Override
public void onBackPressed() {
if(del.isEnabled()){
for (Button b : btn){
b.setVisibility(View.INVISIBLE);
}
del.setVisibility(View.INVISIBLE);
clear.setVisibility(View.INVISIBLE);
}else
super.onBackPressed();
}
public void setTv(String str, int count){
switch (count){
case 0:
tv[0].setText(str);
break;
case 1:
tv[1].setText(str);
break;
case 2:
tv[2].setText(str);
break;
case 3:
tv[3].setText(str);
break;
}
}
}
For brevity, I've only chosen five buttons.为简洁起见,我只选择了五个按钮。
MainActivity主要活动
public class MainActivity extends Activity {
List<String> values = new ArrayList<>();
String[] values = new String[]{" ", " ", " ", " "};
TextView textView[] = new TextView[4];
int size = 0;
@Override
protected void onCreate(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 below
public void onClick(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 here
break;
}
}
private void clearButton(int number) {
switch (number) {
case 0:
button0.setEnabled(true);
break;
case 1:
button1.setEnabled(true);
break;
case 2:
button2.setEnabled(true);
break;
case 3:
button3.setEnabled(true);
break;
case 4:
button4.setEnabled(true);
break;
}
}
public void bindText() {
for (int i = 0; i < 4; i++) {
textView[i].setText(values[i]);
}
}
public void setText(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"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/text_1"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:padding="4dp"/>
<TextView
android:id="@+id/text_2"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:padding="4dp"/>
<TextView
android:id="@+id/text_3"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:padding="4dp"/>
<TextView
android:id="@+id/text_4"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:padding="4dp"/>
</LinearLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:orientation="vertical"
android:rowCount="2">
<Button
android: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"/>
<Button
android: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"/>
<Button
android: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"/>
<Button
android: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"/>
<Button
android: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"/>
<Button
android: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"/>
<Button
android: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. button_selector.xml把它放在你的 drawable 文件夹中。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/green" android:state_enabled="true"/>
<item android:drawable="@color/red" android:state_enabled="false"/>
<item android:drawable="@color/green"/>
</selector>
Logic (as OP asked for it) The values
array stores the numbers that are supposed to be displayed in TextViews.逻辑(按照 OP 的要求) values
数组存储应该在 TextViews 中显示的数字。
When a button is pressed the values array is updated in the setText() method.当按下按钮时,值数组在 setText() 方法中更新。
When clear is pressed this is what happens -当按下 clear 时会发生这种情况 -
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.