カテゴリー:
Timeクラス
閲覧数:477 配信日:2014-02-23 10:02
例1
現在日時を表示
・テキストビューを使って、文字を表示
▼/Time/src/android/style/TimeActivity.java
package android.style; import java.text.TimeFormat; import java.text.ParseException; import java.text.SimpleTimeFormat; import java.util.Calendar; import java.util.Time; import android.app.Activity; import android.app.AlertDialog; import android.app.TimePickerDialog; import android.content.DialogInterface; import android.graphics.Typeface; import android.os.Bundle; import android.text.format.TimeUtils; import android.text.format.Time; import android.view.View; import android.view.ViewGroup; import android.widget.TimePicker; import android.widget.LinearLayout; import android.widget.TextView; public class TimeActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sample1(); public void printString(String s) { // ベースのレイアウト取得 LinearLayout parent = (LinearLayout) findViewById(R.id.parent); TextView tv = new TextView(this); tv.setTypeface(Typeface.MONOSPACE); // 等幅フォントの指定 tv.setText(s); parent.addView(tv); // ビューの追加 } public void sample3() { // android.text.format.Timeクラスでの現在日時 Time time = new Time(); // Timeクラスのコンストラクタには、引数として任意のタイムゾーンを指定することができる。何も指定しない場合は、デフォルトのタイムゾーンとなる time.setToNow(); // Timeオブジェクトに現在日時を設定 String tmp = time.year + "/" + (time.month + 1) + "/" + time.monthDay + " " + time.hour + ":" + time.minute + ":" + time.second; printString(tmp); // タイムゾーンの表示 printString(Time.getCurrentTimezone()); // Time.getCurrentTimezoneメソッド … 引数現在のタイムゾーンを示す文字列を返す } } |
・xml … レイアウト
▼/res/layout/main.xml
<?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:id= "@+id/parent" > </LinearLayout> |