【问题标题】:Android: I can't pass a date to my string IDAndroid:我无法将日期传递给我的字符串 ID
【发布时间】:2012-03-07 14:11:51
【问题描述】:

App 应该有两个 Button 和一个 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
            }


}

【问题讨论】:

  • 这是什么...getDate().toString(R.id.time)???...你的意图是什么?并且您对两个按钮使用相同的引用“btn”..
  • 说真的,你需要先学习如何用java编写代码。这段代码完全是一团糟。

标签: java android date time tostring


【解决方案1】:

试试这个代码 -

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);

试试这个代码。

【讨论】:

  • 我正在尝试获取当前日期,而不是设置一个
  • 我认为这几乎就是他想要的,除了阅读(这并不容易)我认为他有两个按钮,他想更新单个文本视图。例如单击一个按钮,textview 显示当前日期单击另一个按钮,textview 显示当前时间。
【解决方案2】:

为每个按钮创建两个不同的按钮对象和不同的 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
        }
    });

【讨论】:

    猜你喜欢
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 2017-03-26
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    相关资源
    最近更新 更多