简体   繁体   中英

Change TextView depending on time in TextClock Android

I'm trying to change the text of the TextView when the time on TextClock equals to 11:59 PM
I tried doing something like

 if(digitalClock.equals("11:49 PM")){
            tvOne.setText("TEXT CHANGED");
        }


but the TextView did not change so I tried using OnFocusChangeListener on my TextClock

digitalClock.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                System.out.println(digitalClock);
                if(digitalClock.equals("11:59 PM")){
                    tvOne.setText("Text changed");
                }
            }
        });

but I'm not really sure if I'm doing it right.

According to docs, TextClock extends TextView:

 public class TextClock
extends TextView
java.lang.Object
   ↳    android.view.View
       ↳    android.widget.TextView
           ↳    android.widget.TextClock  

Use textClock.getText ().toString().equals(someString) to get text from yours TextClock and compare to string that you want.
And implement TextChangedListener to monitor changes in textClock:

binding.textClock.addTextChangedListener (new TextWatcher () {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (binding.textClock.getText ().toString().equals ("20:43 PM")){
                    binding.textView3.setText (binding.textClock.getText ().toString());
                }
            }
        });

This should work

Binding is Viewbindig feature, to bind data and views

Use Alarm Manager to perform this task

In your onCreate Method

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

// Set the alarm to start at 23:49 PM
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 49);

// setRepeating() lets you specify a precise custom interval--in this case,
// 1 day

Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
if (day == 1) {
            //today = "Sunday";
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, alarmIntent);
            }

Create a receiver class

public class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
                 tvOne.setText("Text changed");

        }
    }

add Permissions

<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" ></uses-permission>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" ></uses-permission>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM