1.listView中添加复杂的数据
在listView中添加复杂的视图时,需要使用Adapter 简单代码:
//activity中
AdapterForConfig adapter = new AdapterForConfig(context, data,
);
listView.setAdapter(adapter);
//adapter的简单代码
public class AdapterForConfig extends BaseAdapter {
public AdapterForConfig(Context context, ConfigInfo data,
) {
}
// 返回值为要显示的行数
@Override
public int getCount() {
// TODO Auto-generated method stub
return config.type_info.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return config.type_info.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
// 最重要的的函数 确定每行要显示的内容及数据
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
//获取xml的视图 每行都会显示该视图
LinearLayout main_layout = (LinearLayout) inflater.inflate(R.layout.listitem_config, null);
return main_layout;
}
}
2.简单的对话框显示
//对话框中使用自定义的view
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View view = View.inflate(context, R.layout.dialog_set_time, null);
builder.setView(view);
点击确定按钮的响应事件
builder.setPositiveButton("确 定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
//显示dialog
Dialog dialog = builder.create();
dialog.show();
// 自定义复杂对话框
Score_Dialog score_dialog = new Score_Dialog(context,
data);
score_dialog.show();
public class Score_Dialog extends Dialog {
public Score_Dialog(Context context, SectionInfo data) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
this.data = data;
inflate = LayoutInflater.from(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
// 全屏模式
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(inflate.inflate(R.layout.dialog_score, null));
super.onCreate(savedInstanceState);
}
}
3.fragment间传递数据
fragment1中:
Bundle bundle = new Bundle();
bundle.putSerializable(
context.getResources().getString(
R.string.tag), data);
fragment2.setArguments(bundle);
fragment2中:
Bundle bundle = this.getArguments();
this.data = (DetailInfo) bundle.get(getActivity().getResources().getString(R.string.tag));
4.view 的gravity属性
5.Calendar的一些简单使用
date.setTime(current.getTimeInMillion());
6.下拉列表的简单使用
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.spinner_text, ctype); // R.layout.spinner_test 列表项
adapter.setDropDownViewResource(R.layout.spinner_drop_style);//spinner_drop_style 下拉列表的样式 可以在这个里面修改显示的宽度 高度</span>
spinner = (Spinner) main_layout.findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener..... //添加spinner选项点击监听
7.textView字体加粗
英文字体加粗:
xml中android:typeface = "bold"即可
中文字体加粗:
text.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);;// 加粗
8.获取网络状态
// 需要添加权限在androidMainFest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
public int getNetworkType() {
int netType = 0;
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_MOBILE) {
String extraInfo = networkInfo.getExtraInfo();
if (extraInfo.toLowerCase().equals("cmnet")) {
netType = NETTYPE_CMNET;
} else {
netType = NETTYPE_CMWAP;
}
} else if (nType == ConnectivityManager.TYPE_WIFI) {
netType = NETTYPE_WIFI;
}
return netType;
}
// 检测手机网络状态
switch (getNetworkType()) {
case 0 :
// Toast.makeText(this, "您目前没有网络连接,程序自动进入离线模式。重新打开程序进入在线模式",
// Toast.LENGTH_LONG).show();
SysApplication.getInstance().setNetState(true);
break;
case 1 :
// Toast.makeText(this, "您正在使用wifi浏览数据", Toast.LENGTH_LONG).show();
SysApplication.getInstance().setNetState(false);
break;
default :
// Toast.makeText(this, "您正在使用移动网络浏览数据", Toast.LENGTH_LONG).show();
SysApplication.getInstance().setNetState(false);
}
9.添加首选项文件
文件名,
MODE_PRIVATE);
String passWord = Base64.encodeToString(txt_password.getText()
.toString().getBytes(), Base64.DEFAULT); // base64转换
pre.edit().putString(getResources().getString(R.string.shared_password),passWord).commit();
null) != null) {
byte b[] = Base64.decode(
pre.getString(
getResources().getString(R.string.shared_password),
null).getBytes(), Base64.DEFAULT);
String info = new String(b);
}
10.自定义标题栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); // 取消上方信息栏
<item name="android:background">#778899</item>//标题栏的背景色
</style>
<style name="ModifyTitleBar" parent="android:Theme.Light">
<item name="android:windowTitleSize">40dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
11.退出程序
/**
* 保存程序的状态
*
*/
public class SysApplication extends Application {
// 运用list来保存们每一个activity是关键
private List<Activity> mList = new LinkedList<Activity>();
// 为了实现每次使用该类时不创建新的对象而创建的静态对象
private static SysApplication instance;
private Context main_context;
// 构造方法
private SysApplication() {
}
// 实例化一次
public synchronized static SysApplication getInstance() {
if (null == instance) {
instance = new SysApplication();
}
return instance;
}
public void setContext(Context context) {
this.context = context;
}
// add Activity
public void addActivity(Activity activity) {
mList.add(activity);
}
// 关闭每一个list内的activity
public void exit() {
try {
for (Activity activity : mList) {
if (activity != null)
activity.finish();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.exit(0);
}
}
// 杀进程
public void onLowMemory() {
super.onLowMemory();
System.gc();
}
}
12.禁止屏幕旋转
13.输入法弹出设置
14.屏蔽系统的物理按键
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true); // 返回键使程序进入后台 不退出
}
return true;
} else if (keyCode == KeyEvent.KEYCODE_MENU) {
openMenu();
return true;
}
return super.onKeyDown(keyCode, event);
}
15.判断程序是否在后台运行
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if(tasksInfo.size() > 0){
System.out.println("---------------包名-----------"+tasksInfo.get(0).topActivity.getPackageName());
//应用程序位于堆栈的顶层
if(packageName.equals(tasksInfo.get(0).topActivity.getPackageName())){
//前台
} else {
16.修改程序的快捷方式(只适用于原生的android系统)
public class ShortCut {
private Context cx;
private int activityCount;
public ShortCut(Context context, int unReadActivityCount) {
this.cx = context;
this.activityCount = unReadActivityCount;
}
/**
* 为当前应用添加桌面快捷方式
*
* @param cx
* @param appName
* 快捷方式名称
*/
public void addShortcut() {
Intent shortcut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
Intent shortcutIntent = cx.getPackageManager()
.getLaunchIntentForPackage(cx.getPackageName());
shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// 获取当前应用名称
String title = null;
try {
final PackageManager pm = cx.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(cx.getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
}
// 快捷方式名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
// 不允许重复创建(不一定有效)
shortcut.putExtra("duplicate", false);
// 快捷方式的图标
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,
generatorContactCountIcon(((BitmapDrawable) (cx.getResources()
.getDrawable(R.drawable.ic_launcher))).getBitmap()));
cx.sendBroadcast(shortcut);
}
/**
* 判断桌面是否已添加快捷方式
*
* @param cx
* @param titleName
* 快捷方式名称
* @return
*/
public boolean hasShortcut() {
boolean result = false;
// 获取当前应用名称
String title = null;
try {
final PackageManager pm = cx.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(cx.getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
}
final String uriStr;
if (android.os.Build.VERSION.SDK_INT < 8) {
uriStr = "content://com.android.launcher.settings/favorites?notify=true";
} else {
uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
}
final Uri CONTENT_URI = Uri.parse(uriStr);
final Cursor c = cx.getContentResolver().query(CONTENT_URI, null,
"title=?", new String[]{title}, null);
if (c != null && c.getCount() > 0) {
result = true;
c.close();
}
return result;
}
/**
* 删除当前应用的桌面快捷方式
*
* @param cx
*/
public void delShortcut() {
Intent shortcut = new Intent(
"com.android.launcher.action.UNINSTALL_SHORTCUT");
// 获取当前应用名称
String title = null;
try {
final PackageManager pm = cx.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(cx.getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
}
// 快捷方式名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
Intent shortcutIntent = cx.getPackageManager()
.getLaunchIntentForPackage(cx.getPackageName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
cx.sendBroadcast(shortcut);
}
/**
* 创建快捷方式的图标
*
* @param icon
* @return
*/
private Bitmap generatorContactCountIcon(Bitmap icon) {
// 初始化画布
int iconSize = (int) cx.getResources().getDimension(
android.R.dimen.app_icon_size);
Bitmap contactIcon = Bitmap.createBitmap(iconSize, iconSize,
Config.ARGB_8888);
Canvas canvas = new Canvas(contactIcon);
// 拷贝图片
Paint iconPaint = new Paint();
iconPaint.setDither(true);// 防抖动
iconPaint.setFilterBitmap(true);// 用来对Bitmap进行滤波处理,这样,当你选择Drawable时,会有抗锯齿的效果
Rect src = new Rect(0, 0, icon.getWidth(), icon.getHeight());
Rect dst = new Rect(0, 0, iconSize, iconSize);
canvas.drawBitmap(icon, src, dst, iconPaint);
// 启用抗锯齿和使用设备的文本字距
Paint countPaint = new Paint(Paint.ANTI_ALIAS_FLAG
| Paint.DEV_KERN_TEXT_FLAG);
countPaint.setColor(Color.RED);
countPaint.setTextSize(20f);
countPaint.setTypeface(Typeface.DEFAULT_BOLD);
if (activityCount != 0) {
// 画圆
canvas.drawCircle(iconSize - 10, 10, 10, countPaint);
// 绘制文本
countPaint.setColor(Color.WHITE);
countPaint.setTextSize(15);
canvas.drawText(String.valueOf(activityCount), iconSize - 15, 15,
countPaint);
}
return contactIcon;
}
}