如何禁用可更改的TextView并单击它以重新启用按钮? [英] how can I disable the changable TextView and click on it reenable the button?

查看:58
本文介绍了如何禁用可更改的TextView并单击它以重新启用按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一个带有绿色图片的按钮键盘,如果用户按下它会禁用它(红色)并在文本视图上打印它,我有4个textview,所以逐步4个文本视图填充最后一个textview不会站立,这是我自己的号码,每次我按另一个启用号码时它都会更改,我如何禁用此号码我想当我单击每个textview删除其中的数字并重新启用其中的数字时,例如,我选择第二个txt2并通过单击数字清除它,方法是在第二个textview上按一个数字自动看照片

Hey I have a button keyboards with green picture if the user press on it disable it(red color) and print it on the text view I have 4 textview so step by step the 4 text view fill the last textview doesn't stand and it's own number its changes every time I press on another enable number how can i disable this and I want when I click on every textview to delete the number in it and reenable the number that was in it and for example i choose second txt2 and i cleared it by click on it by pressing on a number goes automatically on the second textview take a look at the picture

Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
Button b9;
Button b0;
Button ent;
Button clr;
String[] values = new String[]{" ", " ", " ", " "};
TextView textView[] = new TextView[4];
int size = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    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);



    b1 = (Button) findViewById(R.id.button_1);
    b2 = (Button) findViewById(R.id.button_2);
    b3 = (Button) findViewById(R.id.button_3);
    b4 = (Button) findViewById(R.id.button_4);
    b5 = (Button) findViewById(R.id.button_5);
    b6 = (Button) findViewById(R.id.button_6);
    b7 = (Button) findViewById(R.id.button_7);
    b8 = (Button) findViewById(R.id.button_8);
    b9 = (Button) findViewById(R.id.button_9);
    b0 = (Button) findViewById(R.id.button_0);
    ent = (Button) findViewById(R.id.button_enter);
    clr = (Button) findViewById(R.id.button_clear);
    buttonEffect(b0);
    buttonEffect(b1);
    buttonEffect(b2);
    buttonEffect(b3);
    buttonEffect(b4);
    buttonEffect(b5);
    buttonEffect(b6);
    buttonEffect(b7);
    buttonEffect(b8);
    buttonEffect(b9);
    buttonEffect(clr);
    buttonEffect(ent);

}




public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button_0:
            setText(0);
            b0.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
            b0.setEnabled(false);

            break;
        case R.id.button_1:

            setText(1);
            b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
            b1.setEnabled(false);

            break;
        case R.id.button_2:
            setText(2);
            b2.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
            b2.setEnabled(false);

            break;
        case R.id.button_3:
            setText(3);
            b3.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
            b3.setEnabled(false);

            break;
        case R.id.button_4:
            setText(4);
            b4.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
            b4.setEnabled(false);

            break;
        case R.id.button_5:
            setText(5);
            b5.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
            b5.setEnabled(false);

            break;
        case R.id.button_6:
            setText(6);
            b6.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
            b6.setEnabled(false);

            break;
        case R.id.button_7:
            setText(7);
            b7.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
            b7.setEnabled(false);

            break;
        case R.id.button_8:
            setText(8);
            b8.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
            b8.setEnabled(false);

            break;


        case R.id.button_9:
            setText(9);
            b9.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
            b9.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:
            break;
    }
}

private void clearButton(int number) {
    switch (number) {
        case 0 : b0.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
            b0.setEnabled(true);
            break;
        case 1 : b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
            b1.setEnabled(true);

            break;
        case 2 : b2.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
            b2.setEnabled(true);

            break;
        case 3 : b3.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
            b3.setEnabled(true);

            break;
        case 4 : b4.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
            b4.setEnabled(true);

            break;
        case 5 : b5.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
            b5.setEnabled(true);

            break;
        case 6 : b6.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
            b6.setEnabled(true);

            break;
        case 7 : b7.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
            b7.setEnabled(true);

            break;
        case 8 : b8.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
            b8.setEnabled(true);

            break;

        case 9 : b9.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
            b9.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();
}



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;
        }
    });



}

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="450dp"
    android:gravity="bottom"
    android:orientation="vertical"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:id="@+id/linearLayout"
    android:weightSum="1"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">


    <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:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:minHeight="80dp"
            android:background="@drawable/button_selector"
            android:text="1"
            android:textColor="#333"
            android:textSize="25sp" />

        <Button
            android:id="@+id/button_2"
            android:onClick="onClick"
            android:background="@drawable/button_selector"

            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_marginLeft="2dp"
            android:layout_weight="1"
            android:minHeight="80dp"
            android:text="2"
            android:textColor="#333"
            android:textSize="25sp" />

        <Button
            android:id="@+id/button_3"
            android:onClick="onClick"
            android:background="@drawable/button_selector"

            android:layout_width="wrap_content"
            android:layout_height="150dp"
            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="150dp"
            android:layout_weight="1"
            android:background="#112"
            android:minHeight="80dp"
            android:text="clear"
            android:onClick="onClick"

            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:onClick="onClick"
            android:background="@drawable/button_selector"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:minHeight="80dp"
            android:text="4"
            android:textColor="#333"
            android:textSize="25sp" />

        <Button
            android:id="@+id/button_5"
            android:onClick="onClick"
            android:background="@drawable/button_selector"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_marginLeft="2dp"
            android:layout_weight="1"
            android:minHeight="80dp"
            android:text="5"
            android:textColor="#333"
            android:textSize="25sp" />

        <Button
            android:id="@+id/button_6"
            android:onClick="onClick"
            android:background="@drawable/button_selector"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            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/button_enter"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:background="#ccc"
            android:minHeight="80dp"
            android:text="enter"
            android:onClick="onClick"
            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"
        android:layout_weight="0.40">

        <Button
            android:id="@+id/button_7"
            android:onClick="onClick"
            android:background="@drawable/button_selector"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:minHeight="80dp"
            android:text="7"
            android:textColor="#333"
            android:textSize="25sp" />

        <Button
            android:id="@+id/button_8"
            android:onClick="onClick"
            android:background="@drawable/button_selector"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_marginLeft="2dp"
            android:layout_weight="1"
            android:minHeight="80dp"
            android:text="8"
            android:textColor="#333"
            android:textSize="25sp" />

        <Button
            android:id="@+id/button_9"
            android:onClick="onClick"
            android:background="@drawable/button_selector"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            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/button_0"
            android:onClick="onClick"
            android:background="@drawable/button_selector"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            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="wrap_content"
    android:orientation="horizontal"
    android:weightSum="4"

    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <TextView
        android:id="@+id/text_1"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:gravity="center"
        android:textSize="50dp"
        android:freezesText="true"
        android:background="@drawable/greenww"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/text_2"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:gravity="center"
        android:background="@drawable/greenww"
        android:layout_weight="1"
        android:textSize="50dp"
        android:freezesText="true"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"/>
    <TextView
        android:id="@+id/text_3"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:background="@drawable/greenww"
        android:gravity="center"
        android:layout_weight="1"
        android:textSize="50dp"
        android:freezesText="true"

        android:layout_marginRight="5dp"/>
    <TextView
        android:id="@+id/text_4"
        android:layout_width="0dp"
        android:background="@drawable/greenww"
        android:layout_height="150dp"
        android:gravity="center"
        android:textSize="50dp"
        android:freezesText="true"
        android:layout_weight="1"/>


</LinearLayout>

推荐答案

Sparse ... Array 非常有用,因为它们可以同时充当 Map 如果要使用原始数据类型,请避免自动装箱.有关可用方法的更多信息,请查看引用.

Sparse...Arrays are very useful because they can act as a kind of Map while avoiding autoboxing if you're working with primitive data types. For further information regarding available methods, please take a look at the reference.

以下代码在两个地方使用了 SparseIntArray :对于 Button s,我们希望将资源ID映射到每个 Button 的编号.

The following code uses SparseIntArray in two places: for Buttons, we want to map the resource id to the number of each Button.

对于 TextView ,我们需要跟踪它们的状态:它们是否被填充,如果可以的话,用哪个数字填充?因此,我们仅将 SparseIntArray 用于填充的 TextView . textView 的索引是键,数字是值.

For TextViews, we need to keep track of their state: are they filled, and if so, with which number? So we use a SparseIntArray only for filled TextViews. The indices from textView are the keys and the numbers are the values.

Activity 将处理 Button TextView 上的点击.不再需要 values 数组, size 也是如此.

The Activity will handle clicks on Buttons as well as TextViews. The values array is no longer necessary, the same goes for size.

所以这是整个 Activity 代码-享受:)

So here's the entire Activity code - enjoy :)

private static final String EMPTY = " ";

private TextView textView[] = new TextView[4];

private SparseIntArray mapOfButtons,  // map id to number
        mapOfFilledTextViews;         // map array index to value if TextView is filled

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_a8__button_on_touch_and_on_click);

    initMaps();

    textView[0] = (TextView) findViewById(R.id.text_1);
    textView[1] = (TextView) findViewById(R.id.text_2);
    textView[2] = (TextView) findViewById(R.id.text_3);
    textView[3] = (TextView) findViewById(R.id.text_4);

    for (TextView tv: textView)
    {
        tv.setOnClickListener(this);
    }

    Button b1 = (Button) findViewById(R.id.button_1);
    Button b2 = (Button) findViewById(R.id.button_2);
    Button b3 = (Button) findViewById(R.id.button_3);
    Button b4 = (Button) findViewById(R.id.button_4);
    Button b5 = (Button) findViewById(R.id.button_5);
    Button b6 = (Button) findViewById(R.id.button_6);
    Button b7 = (Button) findViewById(R.id.button_7);
    Button b8 = (Button) findViewById(R.id.button_8);
    Button b9 = (Button) findViewById(R.id.button_9);
    Button b0 = (Button) findViewById(R.id.button_0);
    Button ent = (Button) findViewById(R.id.button_enter);
    Button clr = (Button) findViewById(R.id.button_clear);

    buttonEffect(b0);
    buttonEffect(b1);
    buttonEffect(b2);
    buttonEffect(b3);
    buttonEffect(b4);
    buttonEffect(b5);
    buttonEffect(b6);
    buttonEffect(b7);
    buttonEffect(b8);
    buttonEffect(b9);
    buttonEffect(clr);
    buttonEffect(ent);
}

@Override
public void onClick(View v)
{
    int id = v.getId();

    if (v instanceof Button)
    {
        if (mapOfButtons.indexOfKey(id) < 0)
        { // so this is no number button
            if (id == R.id.button_clear)
            {
                if (mapOfFilledTextViews.size() == 0)
                {
                    return;
                }
                // get the key = the index for 'textView'
                int indexToClear = indexOfRightmostFilledTextView();
                // get the value
                int numberToClear = mapOfFilledTextViews.get(indexToClear);
                clearButton(numberToClear);
                clearTextView(indexToClear);
                bindText();
            }
            else if (id == R.id.button_enter)
            {
                // do something
            }
        }
        else // this is a number button
        {
            // only accept clicks if there is a free TextView
            if (mapOfFilledTextViews.size() == textView.length)
            {
                Toast.makeText(this,
           "First clear one of the selected numbers", Toast.LENGTH_SHORT).show();
                return;
            }
            Button b = (Button)v;
            b.setBackgroundResource(R.drawable.redww);
            b.setEnabled(false);

            int number = mapOfButtons.get(id);
            setText(number);
        }

    }
    else if (v instanceof TextView)
    {
        TextView tv = (TextView)v;
        int number = Integer.parseInt(tv.getText().toString() );
        // 'indexOfValue()' will work because we don't have duplicate values
        int i = mapOfFilledTextViews.indexOfValue(number);
        int key = mapOfFilledTextViews.keyAt(i);
        clearButton(number);
        clearTextView(key);
        bindText();
    }
}

private 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;
        }
    });
}

private void initMaps()
{
    // empty in the beginning:
    mapOfFilledTextViews = new SparseIntArray(4);

    mapOfButtons = new SparseIntArray(10);
    mapOfButtons.put(R.id.button_0, 0);
    mapOfButtons.put(R.id.button_1, 1);
    mapOfButtons.put(R.id.button_2, 2);
    mapOfButtons.put(R.id.button_3, 3);
    mapOfButtons.put(R.id.button_4, 4);
    mapOfButtons.put(R.id.button_5, 5);
    mapOfButtons.put(R.id.button_6, 6);
    mapOfButtons.put(R.id.button_7, 7);
    mapOfButtons.put(R.id.button_8, 8);
    mapOfButtons.put(R.id.button_9, 9);
}

private void bindText()
{
    int value;
    int len = textView.length;
    for (int i = 0; i < len; i++)
    {
        value = mapOfFilledTextViews.get(i, -1);
        textView[i].setText(value >= 0 ? String.valueOf(value) : EMPTY);
    }
}

private void setText(int number)
{
    if (mapOfFilledTextViews.size() >= textView.length)
    {
        // this should not happen if we check the number
        // of free TextViews in onClick()
        return;
    }

    int index = indexOfLowestFreeTextView();
    mapOfFilledTextViews.put(index, number);
    bindText();
}

private void clearButton(int number)
{
    // get the first (in our case only) index with valueAt(index) == number
    int index = mapOfButtons.indexOfValue(number);

    if (index < 0) // this value does not exist
    {
        // this should not happen!
        // so maybe throw a RuntimeException
        return;
    }

    int id = mapOfButtons.keyAt(index);
    Button b = (Button) findViewById(id);
    b.setBackgroundResource(R.drawable.greenww);
    b.setEnabled(true);
}

private void clearTextView(int index)
{
    // remove entry as this TextView is no longer filled
    mapOfFilledTextViews.delete(index);
}

/**
 *
 * @return index (for use with 'textView') if there are filled TextViews, else -1
 */
private int indexOfRightmostFilledTextView()
{
    int size = mapOfFilledTextViews.size();
    int maxKey = -1;
    for (int i = 0; i < size; i++)
    {
        int iTemp = mapOfFilledTextViews.keyAt(i);
        if (iTemp > maxKey )
        {
            maxKey = iTemp;
        }
    }
    return maxKey;
}

/**
 * Get index of leftmost (lowest) free TextView
 * @return index for use with 'textView'. 
 * If all TextViews are filled, return textView.length
 */
private int indexOfLowestFreeTextView()
{
    int lowestIndex = textView.length;
    // this assumes the TextViews are sorted from left to right in the textView array
    for (int key = 0; key < textView.length; key++)
    {
        if (mapOfFilledTextViews.indexOfKey(key) < 0)
        {
            lowestIndex = key;
            break;
        }
    }
    return lowestIndex;
}

这篇关于如何禁用可更改的TextView并单击它以重新启用按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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