【发布时间】:2014-04-14 07:57:49
【问题描述】:
我想在我的应用程序片段中的 Textview 中显示当前日期和时间,格式为:DD/MM/YEAR:H:Min 但我不知道如何编写代码,因为我是编程新手。从哪里开始/从哪里开始?
我当前的代码:
package com.example.myshops;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.Fragment;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Monday_fragment extends Fragment implements OnClickListener { private TextView datetimetext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.item1_fragment, container, false);
Button nextpageButton = (Button) v.findViewById(R.id.Item2Button);
nextpageButton.setOnClickListener((OnClickListener) getActivity());
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.nextpageButton:
break;
}
datetimetext.setText("" + DateFormat.format("dd/MM/yyyy kk:mm", System.currentTimeMillis()));
//---Inflate the layout for this fragment---
//return inflater.inflate( R.layout.item1_fragment, container, false);
}
}
【问题讨论】:
-
你的 switch 没有包含 setText,如果你想在每次点击时都使用它,请在调用 switch 之前完成
-
你能给出更具体的解释吗?作为一个初学者,对此一无所知.. @Boe-Dev
-
请检查我的代码中的变化
标签: android date android-fragments textview