Android开发笔记
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开发笔记的更多相关文章
- Android开发笔记:打包数据库
对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...
- Android开发笔记--hello world 和目录结构
原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...
- 【转】Android开发笔记(序)写在前面的目录
原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...
- [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明
接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...
- [APP] Android 开发笔记 002-命令行创建默认项目结构说明
接上节:[APP] Android 开发笔记 001 4. 默认项目结构说明: 这里我使用Sublime Text 进行加载.
- Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计
Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...
- Android开发笔记(一百三十四)协调布局CoordinatorLayout
协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...
- 【转】Android开发笔记——圆角和边框们
原文地址:http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ Android开发笔记——圆角和边框们 在做Androi ...
- 《ArcGIS Runtime SDK for Android开发笔记》
开发笔记之基础教程 ArcGIS Runtime SDK for Android 各版本下载地址 <ArcGIS Runtime SDK for Android开发笔记>——(1).And ...
- 《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:概述
1.前言 数据生产和数据展示是常见的两大专业级移动GIS应用场景,这里我们针对数据生产环节的ArcGIS的离在线一体化技术给大家做一个基本的介绍和梳理. 使用ArcGIS离在线一体化技术首先需要以下基 ...
随机推荐
- 网络存储技术介绍(1) ( based on zt)
最近由于某同学微信发了一些网络存储的文章,开始感兴趣,稍微收集了一些 一. 网络存储技术 http://ask.zol.com.cn/q/187044.html (yxr:很老的技术介绍吧) 网络 ...
- 解决OneNote的无法同步的问题
微软的OneNote一直是我比较喜欢的笔记软件,一些复杂的笔记用它来存储比Evernote要强大很多,然而这几天发现一直无法同步,登陆https://onedrive.live.com/看了一下,发现 ...
- UVa 10300 - Ecological Premium
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...
- 当前JS文件中加入其他js文件
注意:在html文件导入a.js时,应该把script></script写在/body>后面,否则 document.write()方法有问题. 在载入页面后,浏览器输出流自动关闭: ...
- ViewPager图片轮转带点的
布局页面设置: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:and ...
- 【MVC】 js,css 压缩
[MVC] js,css 压缩 一. 引用 System.Web.Optimization.dll : 使用 Nuget ,在控制台输入 Install-Package Microsoft.AspNe ...
- 高版本Chrome不支持showModalDialog
高版本Chrome不支持window.showModalDialog,可用window.open代替 var url = "https://www.baidu.com"; var ...
- lr_convert_string_encoding()转码函数
例子:
- TFS与Eclipse、Microsoft Visual Studio等客户端以webservice进行交换。
TFS与eclipse.Microsoft Visual Studio等客户端以webservice进行交互. 参考地址: http://server_ip:8080/tfs/项目区域/version ...
- jQuery Mobile 过渡效果
jQuery Mobile 包含了允许您选择页面打开方式的 CSS 效果. jQuery Mobile 过渡效果 jQuery Mobile 拥有一系列关于如何从一页过渡到下一页的效果. 注释:如需实 ...