Android get current date and show it in TextView

前端 未结 10 2145
清歌不尽
清歌不尽 2021-02-14 09:23

I do not know what is wrong with code below -

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layo         


        
10条回答
  •  无人共我
    2021-02-14 09:42

    if you want to generate a toast which is displaying current time on a button click then use this code.

    bt.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Calendar c = Calendar.getInstance();
                hour = c.get(Calendar.HOUR_OF_DAY);
                min = c.get(Calendar.MINUTE);
                int ds = c.get(Calendar.AM_PM);
                if(ds==0)
                AM_PM="pm";
                else
                AM_PM="am";
    
                Toast.makeText(MainActivity.this, ""+hour+":"+min+AM_PM, Toast.LENGTH_SHORT).show();
    
    
            }
        });
    

提交回复
热议问题