Android 中国SDK:

http://wear.techbrood.com/

Android SDK Manager 代理设置: http://www.cnblogs.com/sunzn/p/4242131.html

使用Intellij Idea 开发Android:

http://www.tuicool.com/articles/a2MNna

得到当前Activity的RelativeLayout

RelativeLayout root = (RelativeLayout)View.inflate(this, R.layout.activity_main, null);

遍历控件

http://www.open-open.com/lib/view/open1391951537129.html

/**
 
* @note 获取该activity所有view
 
* */
 
public List<View> getAllChildViews() {
 
View view = this.getWindow().getDecorView();
 
return getAllChildViews(view);
 
}
 
 
private List<View> getAllChildViews(View view) {
 
List<View> allchildren = new ArrayList<View>();
 
if (view instanceof ViewGroup) {
 
ViewGroup vp = (ViewGroup) view;
 
for (int i = 0; i < vp.getChildCount(); i++) {
 
View viewchild = vp.getChildAt(i);
 
allchildren.add(viewchild);
 
allchildren.addAll(getAllChildViews(viewchild));
 
}
 
}
 
return allchildren;
 
}

获取最上层Activity

http://blog.sina.com.cn/s/blog_4c0706560101063n.html

 String getTopActivity(Activity context)
{
ActivityManager manager = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE) ;
List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1) ; if(runningTaskInfos != null)
return (runningTaskInfos.get(0).topActivity).toString() ;
else
return null ;
}

最后不要忘记在AndroidManifest.xml中增加权限:

<uses-permission android:name = “android.permission.GET_TASKS”/>

这个方法可能没有用,我所需要的是获取当前程序的当前Activity

网格权限

socket failed:EACCES(Permission denied)
Android净干这脱了裤子放屁的事,写个代码,还要指定权限,就不让程序员省心。

做一个最全的权限,放到 AndroidManifest 里吧。

官方定义:http://developer.android.com/reference/android/Manifest.permission.html

中文:http://blog.sina.com.cn/s/blog_798c1d510101d744.html

<uses-permission android:name="android.permission.INTERNET" />

Ajax助手类

让我们实现一个类似于 jQuery 的Ajax 方法吧。 万恶的Java没有实现这个方法。

package cmn;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.widget.Toast; import com.google.gson.Gson; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils; import java.util.ArrayList;
import java.util.List;
import java.util.Map; import dzx.cn.dzx.R; /**
* Created by Administrator on 2015/7/14.
*/
public class NetHelper {
public static void Post(Activity context,String Url, StringDict dict, IAjaxCallback callback) {
new NetExecuter().Post(context,Url, dict, callback);
}
} class NetExecuter {
private String ErrorMsg; public void Post(final Activity context, String Url, final StringDict Dict, final IAjaxCallback Callback) {
ErrorMsg = ""; AsyncTask<String, StringDict, String> task = new AsyncTask<String, StringDict, String>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
} @Override
protected String doInBackground(String... params) {
String MyUrl = params[0];
HttpPost httpRequest = new HttpPost(MyUrl);
HttpResponse httpResponse = null;
List<NameValuePair> param = new ArrayList<NameValuePair>();
if (Dict != null) {
for (final Map.Entry<String, String> entry : Dict.entrySet()) {
param.add(new NameValuePair() {
@Override
public String getName() {
return entry.getKey();
} @Override
public String getValue() {
return entry.getValue();
}
});
}
} try {
httpRequest.setEntity(new UrlEncodedFormEntity(param, HTTP.UTF_8));
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000);
httpResponse = client.execute(httpRequest); int status = httpResponse.getStatusLine().getStatusCode();
String strResult = EntityUtils.toString(httpResponse.getEntity()); return strResult;
} catch (Exception e) {
ErrorMsg = e.getMessage();
return "";
}
} @Override
protected void onPostExecute(String result) {
super.onPostExecute(result); if( ErrorMsg.isEmpty()== false){
Toast.makeText(context.getApplicationContext(), ErrorMsg,
Toast.LENGTH_SHORT).show();
return;
}
Callback.exec(new Gson().fromJson(result, JsonMsg.class));
}
}; task.execute(Url);
}
}

调用:

NetHelper.Post("http://192.168.1.33/dzx/ajax/city/getCityInfo/1", null, new IAjaxCallback() {
@Override
public void exec(JsonMsg jm) {
TextView txt = (TextView) findViewById(R.id.textView);
txt.setText(jm.GetString("Name"));
}
});

ListInt 助手类定义

当我发现 Java 不能定义 List<int>,我疯了。Java 里居然有 int 和 Integer !这是怎样的一群人造出的Java这个杂种!

定义一个ListInt 助手类吧。

package Base;

import java.util.ArrayList;

/**
* Created by udi on 2015/7/20.
*/
public class ListInt {
private ArrayList<Object> data = new ArrayList<Object>();
public int getLength()
{
return data.size();
} public int Get(int Index){
return (int) data.get(Index);
} public void Add(int Value){
data.add(Value);
} public void AddRange(ListInt Value){
for(int i=0;i< Value.getLength();i++){
data.add( Value.Get(i)) ;
}
} public void Insert(int Index,int Value){
data.add(Index,Value);
} public void RemoveAt(int Index) {
data.remove(Index);
} public void Clear(){
data.clear();
} public int[] ToArray(){
int[] retVal = new int[data.size()];
for(int i=0;i< data.size();i++){
retVal[i] = (int)data.get(i);
}
return retVal;
}
}

ActionBarActivity基类

主要实现 GetJson,SetJson。

package Base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBarActivity;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import cmn.StringDict;
import dzx.cn.dzx.R; /**
* Created by Administrator on 2015/7/14.
*/
public class MyActionBarActivity extends ActionBarActivity { protected Activity context = this; protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} public void SetJson(String Prefix, HashMap<String, String> Model) {
for (Map.Entry<String, String> entry : Model.entrySet()) {
String key = entry.getKey();
if (Prefix != null && key.startsWith(Prefix) == false) {
continue;
} String value = entry.getValue(); int ResID = GetResID(key);
if (ResID <= 0) continue; View view = findViewById(ResID);
if (view == null) continue; if (view instanceof TextView) {
((TextView) view).setText(value);
}
}
}
public RelativeLayout GetRoot(){
return (RelativeLayout)View.inflate(this, getResources().getIdentifier("activity_main", "layout", getPackageName()) , null);
}
public StringDict GetJson(String Prefix) {
StringDict dict = new StringDict(); ListInt views = GetAllTextView(GetRoot()); for (int id : views.ToArray()) {
String key = getResources().getResourceName(id);
if( key.contains("/")){
String[] each = key.split("/") ;
key = each[each.length -1];
} if( Prefix != null && key.startsWith(Prefix) == false)
{
continue;
} String val =((TextView) findViewById(id)).getText().toString();
dict.put(key,val);
}
return dict;
} public int GetResID(String ResKey) {
return getResources().getIdentifier(ResKey, "id", getPackageName());
} private ListInt GetAllTextView(ViewGroup root) {
if (root == null) return new ListInt();
ListInt list = new ListInt(); for (int len = root.getChildCount() - 1; len >= 0; len--) {
View v = root.getChildAt(len);
if (v instanceof ViewGroup) {
list.AddRange(GetAllTextView((ViewGroup) v));
} else if (v instanceof TextView) {
list.Add(v.getId());
}
} return list;
}
}

对应的C# WinForm方法,更优雅。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; using StringDict = System.Collections.Generic.Dictionary<string, string>; namespace WindowsFormsApplication1
{
public class MyForm : Form
{
public void SetJson(string Prefix, StringDict dict)
{
if (Prefix == null)
{
Prefix = "";
} Func<Control, Control[]> GetControl =null; var _GetControl = new Func<Control, Control[]>(c =>
{
var list = new List<Control>();
if (c.HasChildren)
{
foreach (Control con in c.Controls)
{
list.AddRange(GetControl(con));
}
}
else
{
list.Add(c);
}
return list.ToArray();
});
GetControl = _GetControl; foreach (var c in GetControl(this))
{
dict.All(kv =>
{
if (c.Name == Prefix + kv.Key)
{
c.Text = kv.Value;
return false;
}
return true;
});
}
} public StringDict GetJson(string Prefix)
{
if (Prefix == null)
{
Prefix = "";
}
var dict = new StringDict(); Func<Control, Control[]> GetControl = null; var _GetControl = new Func<Control, Control[]>(c =>
{
var list = new List<Control>();
if (c.HasChildren)
{
foreach (Control con in c.Controls)
{
list.AddRange(GetControl(con));
}
}
else
{
if (c is TextBox)
{
list.Add(c);
}
}
return list.ToArray();
});
GetControl = _GetControl; foreach (var c in GetControl(this))
{
if (c.Name.StartsWith(Prefix))
{
dict[c.Name.Substring(Prefix.Length)] = c.Text;
}
}
return dict;
}
}
}

Android开发笔记的更多相关文章

  1. Android开发笔记:打包数据库

    对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...

  2. Android开发笔记--hello world 和目录结构

    原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...

  3. 【转】Android开发笔记(序)写在前面的目录

    原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...

  4. [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明

    接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...

  5. [APP] Android 开发笔记 002-命令行创建默认项目结构说明

    接上节:[APP] Android 开发笔记 001 4. 默认项目结构说明: 这里我使用Sublime Text 进行加载.

  6. Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计

    Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...

  7. Android开发笔记(一百三十四)协调布局CoordinatorLayout

    协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...

  8. 【转】Android开发笔记——圆角和边框们

    原文地址:http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ Android开发笔记——圆角和边框们 在做Androi ...

  9. 《ArcGIS Runtime SDK for Android开发笔记》

    开发笔记之基础教程 ArcGIS Runtime SDK for Android 各版本下载地址 <ArcGIS Runtime SDK for Android开发笔记>——(1).And ...

  10. 《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:概述

    1.前言 数据生产和数据展示是常见的两大专业级移动GIS应用场景,这里我们针对数据生产环节的ArcGIS的离在线一体化技术给大家做一个基本的介绍和梳理. 使用ArcGIS离在线一体化技术首先需要以下基 ...

随机推荐

  1. magnum 命令使用说明

    magnum 命令使用说明 1.用法 usage: magnum [--version] [--debug] [--os-cache] [--os-region-name <region-nam ...

  2. CentOS和Ubuntu下安装配置Greenplum数据库集群(包括安装包和源码编译安装)

    首先说一下,无论是CentOS/RedHat还是Ubuntu都可以按源码方式.安装包方式编译安装. 1.   规划 192.168.4.93(h93)       1个主master  2个主segm ...

  3. DHCP服务器的开始方式

    方法一:采用DHCP服务器接口开启的方式 [Huawei]dhcp enable [Huawei]int g0/0/0[Huawei-GigabitEthernet0/0/0]ip add 192.1 ...

  4. [2015hdu多校联赛补题]hdu5324 Boring Class

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5324 题意:给你一个二维的序列,让你找出最长的第一维升第二维降的子序列(如果多个答案,输出字典序最小) ...

  5. Centos上DNS服务器的简单搭建

    1:安装软件包 yum -y install bind bind-chroot bind-utils bind-libs 2:修改配置文件 1): vim  /etc/named.conf 2):在主 ...

  6. HTML5基础总结

    HTML5是HTML语言的第五次重大版本升级,新增了如下内容:1.新增<video>.<audio>标签在页面上直接播放多媒体资源;2.新增<input>标签的ty ...

  7. modelsim10.0C编译ISE14.7的xilinx库(xilinx ip核)

    1.打开D:\Xilinx\14.7\ISE_DS\ISE\bin\nt64\compxlibgui.exe,nt64表示系统是64位,如果是32位,换成nt,然后按照界面所示一步一步执行, 2.修改 ...

  8. ubuntu 12.04禁用笔记本触摸板

    习惯了在Macbook Pro上使用触摸板,装了个linux 的dualboot,发现,ubuntu下对触摸板的支持实在是太烂了,想禁用触摸板却找不到设置的地方. 终于最后发现了——touchpad- ...

  9. 很实用的jQuery事件 - toggle() 方法

    实例 切换不同的背景色: $("p").toggle( function(){ $("body").css("background-color&quo ...

  10. java核心知识点学习----多线程并发之线程同步

    1.什么是线程同步? 多线程编程是很有趣的事情,它很容易出现"错误情况",这种情况不是由编码造成的,它是由系统的线程调度造成的,当使用多个线程来访问同一个数据时,很容易出现&quo ...