setBackgroundColor(Color.WHITE)在代码的一部分中起作用,但是setBackgroundColor(Color.RED)不起作用

时间:2015-02-19 06:33:52

标签: colors android-imageview

我试图以编程方式更改imageView的颜色,但看起来只有setBackgroundColor(Color.WHITE)正在执行而setBackgroundColor(RED)却没有。

答案:我的RelativeLayout文件中的ImageView被另一个元素掩盖了。

当我调用happybirthdayimage.setBackgroundColor(Color.RED)时,没有任何反应。 Logcat表明应用程序已到达该行代码,但我没有收到任何类型的错误。

当我调用happybirthdayimage.setBackgroundColor(Color.WHITE)时,它的工作正常。

这是为什么?即使有工作,我也想知道为什么我的代码不起作用。

使用Android Studio v 1.0.2 在Nexus 5.0.1上进行测试

我已经包含了testForErrors类和GaussAlgorithm类,但我不认为这些是问题的一部分。

我的主要活动在这里:

package dayOfWeekApp;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;

import com.example.com.piercestudio.R;



public class MainActivity extends Activity {

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


        final Button calculateButton = (Button) findViewById(R.id.calculateButton);
        final TextView resultView = (TextView) findViewById(R.id.resultTextView);
        final EditText editTextField = (EditText) findViewById(R.id.editText);
        final ImageView happybirthdayimage = (ImageView) findViewById(R.id.imageView);

        editTextField.setOnFocusChangeListener(new OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    Log.i(testForErrors.E, "Lost focus, getting it back");
                    v.requestFocus();
                }
            }
        });

        editTextField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {

                    calculateButton.callOnClick();

                    return true;
                }
                return false;
            }
        });


        calculateButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(testForErrors.E, "CLICK");
                String mArray[] = {editTextField.getText().toString(), "error"};

                //error testing:
                mArray = testForErrors.check(mArray);

                if (mArray[1].equals("error")) {

                    Log.i(testForErrors.E, "error passed to main");
                    if(mArray[0].length() == 0){
                        //Intentionally blank so that touching the button does not give errors on pushing the button accidently.
                    }
                    else{

                        //-------

这是imageView应该变成红色的地方

                        happybirthdayimage.setBackgroundColor(Color.RED);
                        Log.i(testForErrors.E, "background RED");

                        //-------

                        calculateButton.setText(mArray[0] + " is not a real date");
                        editTextField.requestFocus();
                    }


                } else {
                    Log.i(testForErrors.E, "no error passed to main");

                    if (mArray[0].startsWith("10/25")) {
                        Log.i(testForErrors.E, "Birthday entered");
                        happybirthdayimage.setBackground(getResources().getDrawable(R.drawable.happybirthday));
                        editTextField.requestFocus();
                        if(editTextField.hasFocus()) {
                            Log.i(testForErrors.E, "editText has focus");
                        }
                        else{
                            Log.i(testForErrors.E, "editText does not have focus");
                        }
                        Log.i(testForErrors.E, "background birthday");
                        editTextField.setText(null);
                        editTextField.requestFocus();
                        Log.i(testForErrors.E, "text to null");
                    } else {

这是转向白色的地方

                        happybirthdayimage.setBackgroundColor(Color.WHITE);
                        Log.i(testForErrors.E, "background WHITE");
                    }
                }
                if (!(mArray[1].equals("error"))) {
                    Log.i(testForErrors.E, "pass to gauss");
                    resultView.setText(GuassAlgorithm.getDate(mArray[0]));

                    calculateButton.setText("I think it's " + GuassAlgorithm.getDate(mArray[0]));
                }
                editTextField.setText(null);
            }

        });


    }
}

XML布局:

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"

android:background="#ffffffff"
tools:context="dayOfWeekApp.MainActivity"
>

<TextView
    android:id="@+id/introText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Welcome! What day was it then? Enter the date in mm/dd/yyyy format and you'll see the answer below!"
    android:textSize="20sp"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:id="@+id/typeDateHere"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:text="Type the date in here:"
    android:paddingBottom="40sp"
    android:paddingTop="10dp"
    android:layout_below="@id/introText"
    android:layout_alignParentStart="true" />

<EditText
    android:focusable="true"
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="50sp"
    android:ems="10"
    android:inputType="date"
    android:layout_below="@+id/typeDateHere"
    android:layout_toEndOf="@+id/imageView">

    <requestFocus />
</EditText>

<Button
    android:id="@+id/calculateButton"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="Click here for the day!"
    android:layout_below="@+id/editText"
    android:layout_alignParentStart="true" />

<TextView
    android:id="@+id/resultTextView"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:text="The day will show up here :)"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_below="@id/calculateButton"
    />

<ImageView
    android:focusable="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_above="@id/editText"
    android:layout_alignParentTop="true"
    android:layout_below="@+id/resultTextView"
    />

检查类和方法时出错:

package dayOfWeekApp;

import android.util.Log;
public class testForErrors {

    public static String E = "TAG";


        public static String[] check(String[] sArray){
        String s = sArray[0];
        sArray[1] = "error";

    if (s == null || s.length() != 10) {
        Log.i(E, "length out of bounds or null");
        return sArray;
    }

    Log.i(E, s.substring(0,2) + s.substring(2,3) + s.substring(3,5) + s.substring(5,6) + s.substring(6));

    try {
        if((Integer.parseInt(s.substring(0, 2)) > 0 && (Integer.parseInt(s.substring(0, 2)) < 13)) != true) {
            Log.i(E, "month out of bounds");
            return sArray;
        }
    } catch (NumberFormatException e) {
        Log.i(E, "month out of bounds error");
        return sArray;
    }
        Log.i(E, "month in bounds");

    int month = Integer.parseInt(s.substring(0, 2));
        Log.i(E, "month = " + Integer.toString(month));

    //this activates only after the month has passed it's test
    //if it were inside the "try" block 1: the rest of the code couldn't see it.
    //10/25/1980

    try {
        Log.i(E, s);
        if(s.substring(2,3).equals("/") != true) {
            Log.i(E, s.substring(2,3));
            Log.i(E, "first slash out of bounds");
            return sArray;
            }
        }
    catch (NumberFormatException e){
        Log.i(E, "first slash error");
        return sArray;
    }
    Log.i(E, "first slash in bounds");

    //day
    try {
        if((Integer.parseInt(s.substring(3, 5)) > 0 && Integer.parseInt(s.substring(3, 5)) < 32) != true){
            Log.i(E, "day out of bounds");
            return sArray;
            }
        }
        catch (NumberFormatException e){
            Log.i(E, "day error");
            return sArray;
        }

    Log.i(E, "day in bounds");

    try{
        if ((month > 1 && month < 13) != true){
            Log.i(E, "month out of bounds");
            return sArray;
        }
    }
        catch(NumberFormatException e){
            Log.i(E, "month error");
            return sArray;
        }
    Log.i(E, "month in bounds");

        switch (month) {
            case 1:
                try {
                    if ((Integer.parseInt(s.substring(0, 2)) > 0) && (Integer.parseInt(s.substring(0, 2)) < 32) != true) {
                        Log.i(E, "Jan out of bounds");
                        return sArray;
                    }
                } catch (NumberFormatException e) {
                    Log.i(E, "Jan error");
                    return sArray;
                }
                Log.i(E, "Jan in bounds");
                break;
            case 2:
                try {
                    if ((Integer.parseInt(s.substring(0, 2)) > 0) && (Integer.parseInt(s.substring(0, 2)) < 29) != true) {
                        Log.i(E, "Feb out of bounds");
                        return sArray;
                    }
                } catch (NumberFormatException e) {
                    Log.i(E, "Feb error");
                    return sArray;
                }
                Log.i(E, "Fed in bounds");
            case 3:
                try {
                    if ((Integer.parseInt(s.substring(0, 2)) > 0) && (Integer.parseInt(s.substring(0, 2)) < 32) != true) {
                        Log.i(E, "Mar out of bounds");
                        return sArray;
                    }
                } catch (NumberFormatException e) {
                    Log.i(E, "Mar error");
                    return sArray;
                }
                Log.i(E, "Mar in bounds");
                break;
            case 4:
                try {
                    if ((Integer.parseInt(s.substring(0, 2)) > 0) && (Integer.parseInt(s.substring(0, 2)) < 31) != true) {
                        Log.i(E, "Apr out of bounds");
                        return sArray;
                    }
                } catch (NumberFormatException e) {
                    Log.i(E, "Apr error");
                    return sArray;
                }
                Log.i(E, "Apr in bounds");
                break;
            case 5:
                try {
                    if ((Integer.parseInt(s.substring(0, 2)) > 0) && (Integer.parseInt(s.substring(0, 2)) < 32) != true) {
                        Log.i(E, "May out of bounds");
                        return sArray;
                    }
                } catch (NumberFormatException e) {
                    Log.i(E, "May error");
                    return sArray;
                }
                Log.i(E, "May in bounds");
                break;
            case 6:
                try {
                    if ((Integer.parseInt(s.substring(0, 2)) > 0) && (Integer.parseInt(s.substring(0, 2)) < 31) != true) {
                        Log.i(E, "Jun out of bounds");
                        return sArray;
                    }
                } catch (NumberFormatException e) {
                    Log.i(E, "Jun error");
                    return sArray;
                }
                Log.i(E, "Jun in bounds");
                break;
            case 7:
                try {
                    if ((Integer.parseInt(s.substring(0, 2)) > 0) && (Integer.parseInt(s.substring(0, 2)) < 32) != true) {
                        Log.i(E, "Jul out of bounds");
                        return sArray;
                    }
                } catch (NumberFormatException e) {
                    Log.i(E, "Jul error");
                    return sArray;
                }
                Log.i(E, "Jul in bounds");
                break;
            case 8:
                try {
                    if ((Integer.parseInt(s.substring(0, 2)) > 0) && (Integer.parseInt(s.substring(0, 2)) < 32) != true) {
                        Log.i(E, "Aug out of bounds");
                        return sArray;
                    }
                } catch (NumberFormatException e) {
                    Log.i(E, "Aug error");
                    return sArray;
                }
                Log.i(E, "Aug in bounds");
                break;
            case 9:
                try {
                    if ((Integer.parseInt(s.substring(0, 2)) > 0) && (Integer.parseInt(s.substring(0, 2)) < 31) != true) {
                        Log.i(E, "Sep out of bounds");
                        return sArray;
                    }
                } catch (NumberFormatException e) {
                    Log.i(E, "Sep error");
                    return sArray;
                }
                Log.i(E, "Sep in bounds");
                break;
            case 10:
                try {
                    if ((Integer.parseInt(s.substring(0, 2)) > 0) && (Integer.parseInt(s.substring(0, 2)) < 32) != true) {
                        Log.i(E, "Oct out of bounds");
                        return sArray;
                    }
                } catch (NumberFormatException e) {
                    Log.i(E, "Oct error");
                    return sArray;
                }
                Log.i(E, "Oct in bounds");
                break;
            case 11:
                try {
                    if ((Integer.parseInt(s.substring(0, 2)) > 0) && (Integer.parseInt(s.substring(0, 2)) < 31) != true) {
                        Log.i(E, "Nov out of bounds");
                        return sArray;
                    }
                } catch (NumberFormatException e) {
                    Log.i(E, "Nov error");
                    return sArray;
                }
                Log.i(E, "Nov in bounds");
                break;
            case 12:
                try {
                    if ((Integer.parseInt(s.substring(0, 2)) > 0) && (Integer.parseInt(s.substring(0, 2)) < 32) != true) {
                        Log.i(E, "Dec out of bounds");
                        return sArray;
                    }
                } catch (NumberFormatException e) {
                    Log.i(E, "Dec error");
                    return sArray;
                }
                Log.i(E, "Dec in bounds");
                break;

        }

    try {
        if(s.substring(5,6).equals("/") != true) {
            Log.i(E, "second slash out of bounds");
            return sArray;
        }
    }
    catch (NumberFormatException e){
        Log.i(E, "second slash error");
        return sArray;
    }
    Log.i(E, "second slash in bounds");

    try {
        if((Integer.parseInt(s.substring(6)) < 1700 && Integer.parseInt(s.substring(6)) < 2600) == true) {
            Log.i(E, "century out of bounds");
            return sArray;
        }
    }
    catch (NumberFormatException e){
        Log.i(E, "century error");
        return sArray;
    }
        sArray[1] = "PASS";
        return sArray;
}

}
package dayOfWeekApp;

public class GuassAlgorithm {


public static String getDate(String s){ //method declaration

    if(s == "error"){
        return "error";
    }

    String dayOfWeek = "some day"; //String declared with variable to test if anything is changing

    int centuryInt = Integer.parseInt(s.substring(6, 8));
    int decadeAndYearInt = Integer.parseInt(s.substring(8, 10));
    int monthInt = Integer.parseInt(s.substring(0, 2));
    int dayInt = Integer.parseInt(s.substring(3, 5));

    Object monthName[][] = {{3, "March"}, {4, "April"}, {5, "May"}, {6, "June"}, {7, "July"}, {8, "August"}, {9, "September"}, {10, "October"}, {11, "November"}, {12, "December"}, {13, "January"}, {14, "February"}};

    switch (monthInt){
    case 1: monthInt = 12; //jan
        break;
    case 2: monthInt = 13; //feb
        break;
    case 3: monthInt = 0; //mar
        break;
    case 4: monthInt = 1; //apr
        break;
    case 5: monthInt = 2; //may
        break;
    case 6: monthInt = 3; //jun
        break;
    case 7: monthInt = 4; //jul
        break;
    case 8: monthInt = 5; //aug
        break;
    case 9: monthInt = 6; //sep
        break;
    case 10: monthInt = 7; //oct
        break;
    case 11: monthInt = 8; //nov
        break;
    case 12: monthInt = 9; //dec
        break;
    }

这是Charles Gauss开发的用于计算星期几的算法。 为什么我对这几个月进行编号,然后立即将其作为m + 1运行,然后再也不再使用它超出了我。这是算法。

        int h = (dayInt + (26*(monthInt+1)/10) + decadeAndYearInt + (decadeAndYearInt/4) + (centuryInt/4) + (5*centuryInt)) % 7;

        String weekDays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        dayOfWeek = weekDays[h];

            return dayOfWeek;
    }
}

**如果你能看到为什么我的editText在happyBirthday图像出现后失去焦点,那么你可以看到奖励积分。

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends Activity {
ImageView happybirthdayimage;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final Button calculateButton = (Button) findViewById(R.id.calculateButton);
    final TextView resultView = (TextView) findViewById(R.id.resultTextView);
    final EditText editTextField = (EditText) findViewById(R.id.editText);
    happybirthdayimage = (ImageView) findViewById(R.id.imageView);

也可能是由XML文件中imageView的demensions引起的。我尝试了它,直到我在图像视图中添加了填充,它才会显示出来。