<TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="85dp" /> </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.view.Menu; import android.app.Activity; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="37dp" android:text="Select Your Time" android:textAppearance="?android:attr/textAppearanceSmall" /> <EditText android:id="@+id/editText1" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="24dp" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:layout_marginTop="41dp" android:text="Select Time" /> </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.app.Activity; import android.app.TimePickerDialog; import android.app.Dialog; import android.view.Menu; import android.widget.Button; import android.widget.EditText; import android.widget.TimePicker; import android.view.View; import java.util.Calendar; public class MainActivity extends Activity { private EditText editT1; private Button btn1; private int mHour; private int mMinute; static final int TIME_DIALOG_ID = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // editText1 editT1 = (EditText) findViewById(R.id.editText1); // button1 btn1 = (Button) findViewById(R.id.button1); // add a click listener to the button btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(TIME_DIALOG_ID); } }); // get the current time final Calendar c = Calendar.getInstance(); mHour = c.get(Calendar.HOUR); mMinute = c.get(Calendar.MINUTE); // display the current time updateCurrentTime(); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case TIME_DIALOG_ID: return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false); } return null; } // updates the time we display in the editText private void updateCurrentTime() { editT1.setText( new StringBuilder() .append(mHour).append(":") .append(mMinute)); } private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub mHour = hourOfDay; mMinute = minute; updateCurrentTime(); } }; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท