Android 개발 시간 날짜 구성 요소 사용법 인 스 턴 스
시간 날짜 설정 구성 요소:TimePicker,DatePicker
레이아웃 파일 에 레이아웃 스타일 에 직접 추가 할 수 있 습 니 다.구체 적 인 코드 는 다음 과 같 습 니 다.
<LinearLayout
android:id="@+id/linear1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/editview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/linear2"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
><br> //
<TimePicker
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> <br> //
<DatePicker
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
.java 파일 코드 는 다음 과 같 습 니 다.
public class MainActivity extends Activity {
private EditText myedit = null;<br> //
private TimePicker time = null;
private DatePicker date = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);<br><br> // Activity
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
myedit = (EditText)findViewById(R.id.editview);<br> //
time = (TimePicker)findViewById(R.id.time);
date = (DatePicker)findViewById(R.id.date);
//time.setIs24HourView(true);// 24 , 12
time.setOnTimeChangedListener(new mytime());<br> // ondatechangedlistener() , init() 。
date.init(this.date.getYear(), this.date.getMonth(), this.date.getDayOfMonth(), new myDate());
this.setDateTime();
}
// : <br><br> class mytime implements OnTimeChangedListener{
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
MainActivity.this.setDateTime();
}
}
<br> //
@SuppressLint("NewApi")
class myDate implements OnDateChangedListener{
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
MainActivity.this.setDateTime();
}
}
public void setDateTime() { //
this.myedit.setText(this.date.getYear() + "-"
+ (this.date.getMonth() + 1) + "-" + this.date.getDayOfMonth()
+ " " + this.time.getCurrentHour() + ":"
+ this.time.getCurrentMinute()); //
}
}
효과 도 는 다음 과 같다.이 예 에서 비교적 어 려 운 점 은 날짜 이벤트 에 OnChanged Listener()방법 이 없다 는 것 이다.여기 서 우 리 는 날짜 의 init()방법 으로 대체 했다.그 다음은 Activity 의 디 스 플레이 방향 을 어떻게 제어 하 는 지 입 니 다.Activity 의 디 스 플레이 방향 은 기본적으로 세로 화면 으로 표 시 됩 니 다.우 리 는 자바 파일 을 통 해 새로운 설정 을 할 수도 있 고 설정 파일 에서 설정 할 수도 있 습 니 다.이것 도 자바 파일 에서 비교적 유연 하 게 설정 할 수 있 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.