应用程序应该有两个按钮和一个TextView,一个用于日期,另一个用于时间。单击日期按钮将在TextView中显示当前日期,并为时间按钮提供相同的日期。 updateTime类是我遇到问题的地方。我尝试过将该日期传递给ID的不同方法。
这是我的源代码和说明:
package com.CS211D.DateAndTime;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.Time;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
/*
* This is the activity for my Android App that gives you the date and time when you click on a button. I'm having trouble trying to figure out the date syntax
*/
//*******************TimeAndDateActivity*******************
public class TimeAndDateActivity extends Activity implements View.OnClickListener
{
Button btn;
DateFormat fmtDateTime = DateFormat.getDateAndTimeInstance(); //other utils I've tried
Calendar mycal = Calendar.getInstance(); //with no avail
@Override
//*******************onCreate*******************
public void onCreate(Bundle b)
{
super.onCreate(b);
btn = (Button)this.findViewById(R.id.time); //Time Button
btn = (Button)this.findViewById(R.id.day); //Date Button
}
//*******************onClick*******************
public void onClick(View v) //Called when one of the buttons above is clicked is clicked
{
updateTime();
}
//*******************updateTime*******************
private void updateTime()
{
t = getTime()
btn.setText(getDate().toString(R.id.time));//Most of the methods I try eclipse says
//the method is either undefined or incompatible
//with the object
}
}
答案 0 :(得分:0)
试试这段代码 -
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
String date = String.valueOf(day) + "-" + String.valueOf(month) + "-" +String.valueOf(year);
btn = (Button)this.findViewById(R.id.day);
btn.setText(date);
试试这段代码。
答案 1 :(得分:0)
为每个按钮创建两个不同的按钮对象和不同的onclick侦听器。
Button butdate = (Button)this.findViewById(R.id.day);
Button buttime = (Button)this.findViewById(R.id.time);
butdate.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v )
{
//code to set date in textview
}
});
buttime.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v )
{
//code to set time in textview
}
});