如何在几个TextViews中输入数字? [英] How can I put numbers In several TextViews?

查看:76
本文介绍了如何在几个TextViews中输入数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有gridview按钮,例如键盘,如果我单击它们,无论如何会变成红色,但它们都会变成绿色,并且我有4个TextView,所以我想当我在textview1上按数字文本时,第二个数字转到txt2,第三个数字txt3到txt4的第四个,但是如果我按另一个数字会自动更改txt4,则必须更改它,并且重要的是我不能使用相同的数字,例如在txt1中我按了数字1按钮变成红色,所以我不能使用它在txt2,3,4中,直到我将其从txt1中删除之前,请看一下我的图片,如果有人想要该代码,我很乐意将其放入

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

(以及一个数字后如何自动跳至下一个textview)

(and how automatically jumps to the next textview after one number)

MainActivity.java这可以帮助我编辑TextView所需的文本

MainActivity.java this helps me with edit text I want it for 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>

推荐答案

为简便起见,我只选择了五个按钮.

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 放置在您的可绘制文件夹中.

button_selector.xml Place this in your drawable folder.

<?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中显示的数字.

Logic (as OP asked for it) The values array stores the numbers that are supposed to be displayed in TextViews.

按下按钮时,将在setText()方法中更新values数组.

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;

这篇关于如何在几个TextViews中输入数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆