嘿,我有像绿色指示键盘的网格视图按钮,如果我点击其中一个变成红色反正我有4个TextViews所以我想按下一个数字文本它在textview1然后第二个数字转到txt2第三个到txt3第四个到txt4但txt4必须是可以改变的,如果我按下另一个数字自动改变,重要的是我不能使用相同的数字例如在txt1我按下数字1按钮变红所以我不能使用它在txt2,3,4直到我从txt1删除它看看我的图片如果有人想要代码我很乐意把它放在
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 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>
答案 0 :(得分: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++;
}
梅托德:
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;
}
}
删除按钮:
if(click != 0) {
tv[--click].setText("");
}
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笔划:
<?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>
主要活动:
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;
}
}
}
答案 1 :(得分:0)
为简洁起见,我只选择了五个按钮。
<强> 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 将其放在可绘制文件夹中。
<?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>
逻辑(OP要求它)
values
数组存储了应该在TextViews中显示的数字。
按下按钮时,值数组将在setText()方法中更新。
当按下清除时,会发生这种情况 -
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;