欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

转化为当前的日期和时间 博客分类: android学习 android转化为当前时间日期 

程序员文章站 2024-03-16 20:16:04
...
xml页面


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"  />

    <Button
        android:id="@+id/btn01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="日期" />
    <Button
        android:id="@+id/btn02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="时间" />

</LinearLayout>


java 代码


public  class NowTimeActivity extends Activity implements OnClickListener,OnKeyListener{
    private EditText et01;
    private Button btn01;
    private Button btn02;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        et01 = (EditText)findViewById(R.id.editText1);
        btn01 = (Button)findViewById(R.id.btn01);
        btn02 = (Button)findViewById(R.id.btn02);

        et01.setOnKeyListener(this);
        btn01.setOnClickListener(this);
        btn02.setOnClickListener(this);

    }

    public void onClick(View v){
       switch(v.getId()){
       case R.id.btn01:
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
           String showDate = sdf.format(new Date());
           et01.setText(showDate);
           break;
       case R.id.btn02:
           SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:SS");
           String showDate2 = sdf.format(new Date());
           et01.setText(showDate2);
           break;
       default:
           break;
       }
    }

    public boolean onKey(View v,int keyCode, KeyEvent event){
       switch(event.getKeyCode()){
       case KeyEvent.KEYCODE_C:
           et01.setText("C键被按下");
           break;
       default:
           break;
       }
       return true;
    }
}