android 自定义日期控件怎么实现的
2015-07-07 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517195
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
自定义DigitalClock的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
>
<TextView
android:id="@+id/textViewTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="55sp"
android:text="10:50:30"
android:layout_gravity="center"
android:lines="1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
>
<TextView
android:id="@+id/textViewWeek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="星期X"
android:lines="1" />
<TextView
android:id="@+id/textViewDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="XXXX年XX月X日"
android:lines="1" />
</LinearLayout>
</LinearLayout>
布局空间设置
DigitalClock
package com.javen.digitalclock;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DigitalClock extends LinearLayout {
static SimpleDateFormat sdf_time = new SimpleDateFormat("hh:mm:ss");
static SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy年MM月dd日");
static Calendar cal = Calendar.getInstance();
private TextView textViewTime, textViewDate, textViewWeek;
// public DigitalClock(Context context, AttributeSet attrs, int defStyle) {
// super(context, attrs, defStyle);
// }
public DigitalClock(Context context, AttributeSet attrs) {
super(context, attrs);
// 使用layoutinflater把布局加载到本ViewGroup
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.digitalcolck_layout, this);
textViewTime = (TextView) findViewById(R.id.textViewTime);
textViewDate = (TextView) findViewById(R.id.textViewDate);
textViewWeek = (TextView) findViewById(R.id.textViewWeek);
startThread();
}
public static String getCurrentTime(Date date) {
sdf_time.format(date);
return sdf_time.format(date);
}
public static String getCurrentDate(Date date) {
sdf_date.format(date);
return sdf_date.format(date);
}
public static String getCurrentWeekDay(Date dt) {
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
private void startThread() {
new Thread(new Runnable() {
public void run() {
while (true) {
handler.sendEmptyMessage(12);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 12) {
Date date = new Date();
textViewTime.setText(getCurrentTime(date));
textViewDate.setText(getCurrentDate(date));
textViewWeek.setText(getCurrentWeekDay(date));
}
}
};
}
mainActivity
package com.javen.digitalclock;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
布局文件main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.javen.digitalclock.DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DigitalClock"
>
</com.javen.digitalclock.DigitalClock>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
>
<TextView
android:id="@+id/textViewTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="55sp"
android:text="10:50:30"
android:layout_gravity="center"
android:lines="1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
>
<TextView
android:id="@+id/textViewWeek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="星期X"
android:lines="1" />
<TextView
android:id="@+id/textViewDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="XXXX年XX月X日"
android:lines="1" />
</LinearLayout>
</LinearLayout>
布局空间设置
DigitalClock
package com.javen.digitalclock;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DigitalClock extends LinearLayout {
static SimpleDateFormat sdf_time = new SimpleDateFormat("hh:mm:ss");
static SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy年MM月dd日");
static Calendar cal = Calendar.getInstance();
private TextView textViewTime, textViewDate, textViewWeek;
// public DigitalClock(Context context, AttributeSet attrs, int defStyle) {
// super(context, attrs, defStyle);
// }
public DigitalClock(Context context, AttributeSet attrs) {
super(context, attrs);
// 使用layoutinflater把布局加载到本ViewGroup
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.digitalcolck_layout, this);
textViewTime = (TextView) findViewById(R.id.textViewTime);
textViewDate = (TextView) findViewById(R.id.textViewDate);
textViewWeek = (TextView) findViewById(R.id.textViewWeek);
startThread();
}
public static String getCurrentTime(Date date) {
sdf_time.format(date);
return sdf_time.format(date);
}
public static String getCurrentDate(Date date) {
sdf_date.format(date);
return sdf_date.format(date);
}
public static String getCurrentWeekDay(Date dt) {
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
private void startThread() {
new Thread(new Runnable() {
public void run() {
while (true) {
handler.sendEmptyMessage(12);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 12) {
Date date = new Date();
textViewTime.setText(getCurrentTime(date));
textViewDate.setText(getCurrentDate(date));
textViewWeek.setText(getCurrentWeekDay(date));
}
}
};
}
mainActivity
package com.javen.digitalclock;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
布局文件main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.javen.digitalclock.DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DigitalClock"
>
</com.javen.digitalclock.DigitalClock>
</LinearLayout>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询