java中获取了当前系统地时间,怎样每秒获取更新一次呢?
这里是代码,请求大神帮忙补充完整,最好是后面加上注释,谢谢!publicclassMainActivityextendsActionBarActivity{publicS...
这里是代码,请求大神帮忙补充完整,最好是后面加上注释,谢谢!
public class MainActivity extends ActionBarActivity
{
public String editText = "";
public Time clock = new Time();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);//创建新的界面(super)属于父类
setContentView(R.layout.activity_main);//创建界面
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{//菜单以及菜单的响应
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
private TextView tvResult=null;//新的TextV
private OnClickListener mListener = new OnClickListener()
{
@Override
public void onClick(View arg0)
{
if (arg0.getId() == R.id.btn1)
{
startActivity(new Intent(PlaceholderFragment.this.getActivity(), AddActivity.class));//启动那个类要告诉startActivity
}
else if (arg0.getId() == R.id.btn2)
{
startActivity(new Intent(PlaceholderFragment.this.getActivity(),SetupActivity.class));
}
}
};
// 构造函数 constructor
public PlaceholderFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String str = formatter.format(curDate);
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
tvResult= (TextView)rootView.findViewById(R.id.tvResult);
tvResult.setText("Time");
tvResult.setText(formatter.format(curDate));
TextView btn1=(TextView)rootView.findViewById(R.id.btn1);
btn1.setText("添加闹钟");
TextView btn2=(TextView)rootView.findViewById(R.id.btn2);
btn2.setText("设置");
tvResult.setOnClickListener(mListener);
btn1.setOnClickListener(mListener);
btn2.setOnClickListener(mListener);
return rootView;
}
};
} 展开
public class MainActivity extends ActionBarActivity
{
public String editText = "";
public Time clock = new Time();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);//创建新的界面(super)属于父类
setContentView(R.layout.activity_main);//创建界面
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{//菜单以及菜单的响应
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
private TextView tvResult=null;//新的TextV
private OnClickListener mListener = new OnClickListener()
{
@Override
public void onClick(View arg0)
{
if (arg0.getId() == R.id.btn1)
{
startActivity(new Intent(PlaceholderFragment.this.getActivity(), AddActivity.class));//启动那个类要告诉startActivity
}
else if (arg0.getId() == R.id.btn2)
{
startActivity(new Intent(PlaceholderFragment.this.getActivity(),SetupActivity.class));
}
}
};
// 构造函数 constructor
public PlaceholderFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String str = formatter.format(curDate);
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
tvResult= (TextView)rootView.findViewById(R.id.tvResult);
tvResult.setText("Time");
tvResult.setText(formatter.format(curDate));
TextView btn1=(TextView)rootView.findViewById(R.id.btn1);
btn1.setText("添加闹钟");
TextView btn2=(TextView)rootView.findViewById(R.id.btn2);
btn2.setText("设置");
tvResult.setOnClickListener(mListener);
btn1.setOnClickListener(mListener);
btn2.setOnClickListener(mListener);
return rootView;
}
};
} 展开
4个回答
推荐于2018-04-12 · 知道合伙人软件行家
关注
展开全部
1、可以写个计数器多线程方式
2、多线程有继承Thread类 和 实现Runnable接口两种实现方式
3、一般会考虑用实现Runnable接口会更好一点,因为Runnable接口能够实现资源的共享
2、多线程有继承Thread类 和 实现Runnable接口两种实现方式
3、一般会考虑用实现Runnable接口会更好一点,因为Runnable接口能够实现资源的共享
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Thread + Handler
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询