How change text view on a given day in Android Studio?

时间:2017-08-04 12:45:58

标签: android textview

I want to create an event such that on a given day that I select a new message will appear. For example: on January 1st, I might want it to say "Hello" and then on January 5th the text changes to "Hey" or some other sort of message that I decide beforehand that will appear on a day I pick. Any ideas on how to do this or where I could look to set me in the right direction?

2 个答案:

答案 0 :(得分:0)

I made this in Eclipse. Same thing will work in Android. Instead of System.out.println(); you do textView.setText();

public class CheckDay {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        GregorianCalendar cal = new GregorianCalendar();

        System.out.println(cal.get(Calendar.DATE)); //prints 4
        System.out.println(cal.get(Calendar.DAY_OF_WEEK)); //6 (weeks start on sunday)
        System.out.println(cal.get(Calendar.YEAR)); //2017
        System.out.println(cal.get(Calendar.MONTH) + 1); //this starts at 0, not 1. Therefore, add 1


        if (cal.get(Calendar.DAY_OF_MONTH) == 4) { //Is it the fourth of the month?
            System.out.println("fourth");
        } else {
            System.out.println("NOT THE FOURTH");
        }

        if ((cal.get(Calendar.DAY_OF_MONTH) == 4) && (cal.get(Calendar.MONTH) + 1 == 8)) { //Is it the fourth of the month?
            System.out.println("fourth of august");
        } else {
            System.out.println("NOT THE FOURTH OF AUGUST");
        }

    }

}

Oh, and this will print the following

4
6
2017
8
fourth
fourth of august

UPDATED FOR ANDROID STUDIO CODE

MainActivity

public class MainActivity extends AppCompatActivity {

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

        TextView textView = (TextView) findViewById(R.id.textView);

        GregorianCalendar cal = new GregorianCalendar();

        System.out.println(cal.get(Calendar.DATE)); //prints 4
        System.out.println(cal.get(Calendar.MONTH) + 1); //this starts at 0, not 1. Therefore, add 1
        System.out.println(cal.get(Calendar.YEAR)); //2017

        if ((cal.get(Calendar.DAY_OF_MONTH) + 0 == 4) && (cal.get(Calendar.MONTH) + 1 == 8)) { //Is it the fourth of the month of 2017?
            System.out.println("TODAY IS AN A DAY");
            textView.setText("Today is August Fourth");
        } else {
            System.out.println("ERROR");
            textView.setText("Today is NOT August Fourth");
        }

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.vzw.www.calendar.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:text="Hello World!"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

答案 1 :(得分:0)

公共课CheckDay {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    GregorianCalendar cal = new GregorianCalendar();

    System.out.println(cal.get(Calendar.DATE)); //prints 4
    System.out.println(cal.get(Calendar.MONTH) + 1); //this starts at 0, not 1. Therefore, add 1
    System.out.println(cal.get(Calendar.YEAR)); //2017

    if ((cal.get(Calendar.DAY_OF_MONTH) + 0 == 4) && (cal.get(Calendar.MONTH) + 1 == 8)) { //Is it the fourth of the month of 2017?
        System.out.println("TODAY IS AN A DAY");
    } else {
        System.out.println("ERROR");
    }

}

}

相关问题