这是代码是在textview中显示消息
Button can1 = (Button) findViewById(R.id.can1);
txt1 = (TextView) findViewById(R.id.txt1);
Calendar c = Calendar.getInstance();
hr1 = c.get(Calendar.HOUR_OF_DAY);
startAlarm();
if(hr1<12)
{
txt1.setText("Good morning!");
}else if(hr1>12&& hr1<17)
{
txt1.setText("it's afternoon!");
}else if(hr1>17&& hr1<20)
{
txt1.setText("Good evening!");
}
can1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void startAlarm() {
if (alarm.getAlarmTonePath() != "") {
mediaPlayer = new MediaPlayer();
if (alarm.getVibrate()) {
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
long[] pattern = { 1000, 200, 200, 200 };
vibrator.vibrate(pattern, 0);
}
try {
mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.setDataSource(this,
Uri.parse(alarm.getAlarmTonePath()));
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mediaPlayer.setLooping(true);
mediaPlayer.prepare();
mediaPlayer.start();
}
}
}
我的显示消息和按钮的布局是
<?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" >
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"/>
<Button
android:id="@+id/can1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:tag="cancel" />
</LinearLayout>
请建议我代码中有什么错误, 我能够显示取消按钮但是,文本(消息)没有显示
答案 0 :(得分:2)
如果hr1高于或等于20,则表示没有显示任何内容。试试这个:
if (hr1 < 12) {
txt1.setText("Good morning!");
} else if(hr1 > 12 && hr1 < 17) {
txt1.setText("it's afternoon!");
} else if(hr1 > 17 && hr1 < 20) {
txt1.setText("Good evening!");
} else {
txt1.setText("Good night!");
}