//Activity
package com.android.Somejavaknowledget;

import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Somejavaknowledget extends Activity {
/** Called when the activity is first created. */
public static final String MyAction="Customized-Action-ChangeText";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);*/
//客制化Title
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_gallery_title);
TextView tv = (TextView)getWindow().findViewById(R.id.left_text);
tv.setText("hello");


DateView dv= (DateView)findViewById(R.id.tv);
dv.setUpdates(true);
Button btn = (Button) getWindow().findViewById(
R.id.close);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.println("finish this activity");
finish();

}
});
Button show = (Button)findViewById(R.id.change);
show.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//自定义的Action
Intent intent= new Intent();
intent.setAction(MyAction);
sendBroadcast(intent);


}
});



}
}


//DateView 继承TextView 通过reciver broadcast 动态改变text

package com.android.Somejavaknowledget;



import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
import android.view.MotionEvent;

import java.text.DateFormat;
import java.util.Date;
import java.util.Formatter;

public final class DateView extends TextView {
private static final String TAG = "DateView";

private boolean mUpdating = false;
private Context mContext;
private static int count=1;


private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
/* if (action.equals(Intent.ACTION_TIME_TICK)
|| action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
updateClock();
}*/
if(action.equalsIgnoreCase(Somejavaknowledget.MyAction)){
updateClock();
}
}

};

public DateView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext=context;
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
setUpdates(false);
}

@Override
protected int getSuggestedMinimumWidth() {
// makes the large background bitmap not force us to full width
return 0;
}

private final void updateClock() {
//Date now = new Date();
//setText(DateFormat.getDateInstance(DateFormat.LONG).format(now));
String s=String.format("count=%d", count);
setText(s);
count++;
}

void setUpdates(boolean update) {
if (update != mUpdating) {
mUpdating = update;
if (update) {
// Register for Intent broadcasts for the clock and battery
IntentFilter filter = new IntentFilter();
/*filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);*/
filter.addAction(Somejavaknowledget.MyAction);
mContext.registerReceiver(mIntentReceiver, filter, null, null);

updateClock();
} else {
mContext.unregisterReceiver(mIntentReceiver);
}
}
}
}







更多相关文章

  1. 轻量级Android6.0动态权限解决方案
  2. Android动态加载插件APK
  3. Java中的ClassLoader 动态加载机制
  4. Android市场动态呈现 最新数据抢先看
  5. Android 球形进度条,动态控制时间与最大进度
  6. android 动态设置 WebView 的宽高

随机推荐

  1. MSDK接入 中的各种问题
  2. Android(安卓)仿360桌面小人
  3. Android(安卓)Activity淡入淡出动画
  4. 短信拦截的解决方案 经典
  5. android SQLiteOpenHelper 对SQLite的操
  6. Android实现两次按下返回键退出
  7. Android(安卓)SDK自带教程之BluetoothCha
  8. Android(安卓)自定义分享列表ACTION_SEND
  9. Android与Js交互获取返回值
  10. android 实时检测网络状态