Android 개발 시간 날짜 작업 실례
이제 구체 적 인 예 시 를 살 펴 보 겠 습 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.
우선 시간 설정:
.java 파일(MainActivity.java)코드 는 다음 과 같 습 니 다.
package com.example.activity_time_date;
import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
private TextView mytext = null;
private Button mybutton1 = null;
private Button mybutton2 = null;
private int mHour;
private int mMinute;
static final int TIME_DIALOG_ID = 0;
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
mHour = hourOfDay;
mMinute = minute;
updateDisplay();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mytext = (TextView)findViewById(R.id.textview);
mybutton1 = (Button)findViewById(R.id.button1);
mybutton2 = (Button)findViewById(R.id.button2);
mybutton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(TIME_DIALOG_ID);
}
});
mybutton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
MainActivity.this.startActivity(new Intent(MainActivity.this, dateActivity.class));
}
});
final Calendar cal = Calendar.getInstance();
mHour = cal.get(Calendar.HOUR_OF_DAY);
mMinute = cal.get(Calendar.MINUTE);
updateDisplay();
}
private void updateDisplay(){
mytext.setText(new StringBuilder().append(pad(mHour)).append(":")
.append(pad(mMinute)));
}
private static String pad(int i){
if (i >= 10)
return String.valueOf(i);
else
return "0" + String.valueOf(i);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,
false);
}
return null;
}
}
레이아웃 파일(activitymain.xml)코드 는 다음 과 같 습 니 다.
<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"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" "
/>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" "
/>
</LinearLayout>
</RelativeLayout>
다음 그림 과 같이 실행 효과:날짜 설정 과 시간 설정 이 기본적으로 일치 합 니 다.더 이상 언급 하지 않 겠 습 니 다.독자 들 은 본 논문 의 예시 코드 를 디 버 깅 하고 개선 할 수 있 으 며 새로운 수확 이 있 을 것 이 라 고 믿 습 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 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에 따라 라이센스가 부여됩니다.