public class Wizard extends Activity { private TextView step1result, step2result, step3result; public static final String INTENT_STEP1 = "com.novoda.STEP1"; public static final String INTENT_STEP2 = "com.novoda.STEP2"; public static final String INTENT_STEP3 = "com.novoda.STEP3"; private static final int STEP1 = 1; private static final int STEP2 = 2; private static final int STEP3 = 3; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.wizard); this.step1result = (TextView)findViewById(R.id.step1result); this.step2result = (TextView)findViewById(R.id.step2result); this.step3result = (TextView)findViewById(R.id.step3result); startActivityForResult(new Intent(Wizard.INTENT_STEP1), STEP1); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case STEP1: this.step1result.setText(data.getStringExtra("STEP1RESULT")); startActivityForResult(new Intent(Wizard.INTENT_STEP2), STEP2); break; case STEP2: this.step2result.setText(data.getStringExtra("STEP2RESULT")); startActivityForResult(new Intent(Wizard.INTENT_STEP3), STEP3); break; case STEP3: this.step3result.setText(data.getStringExtra("STEP3RESULT")); break; } } }
public class Step1 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.step1); Button nextStep = (Button)findViewById(R.id.goto2); nextStep.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent it = new Intent(); it.putExtra("STEP1RESULT", ((EditText)findViewById(R.id.step1value)).getText() .toString()); setResult(Activity.RESULT_OK, it); finish(); } }); } }
在activity中装载了一个view,然后在view中重载了onKeyDown方法,但是按键后没有反应,又试了试onTouchEvent,onTouchEvent有反应。。然后忽然看见一片文章在activity中重载onKeyDow 然后再这里面在调用view的onKeyDow,我就试了试,结果可以。。
然后又查了查看到了这个
当某个键被按下时会触发,但不会被任何的该Activity内的任何view处理。
默认按下KEYCODE_BACK键后会回到上一个Activity。
Activity.onKeyUp():
当某个按键被按下,松开后触发,但不会被任何的该Activity内的任何view处理。
默认没有执行任何操作,只是简单的给一个false作为返回值。
网络链接访问为题
android url访问主要分为两种:一种是httpurlconnection,另一种是httpclient,而前者只是简单的访问,不能设置参数,头文件等,而后者刚好弥补了前者的不足:
/**
* 执行一个HTTP GET请求,返回请求响应的HTML
*
* @param url
* 请求的URL地址
* @param queryString
* 请求的查询参数,可以为null
* @param charset
* 字符集
* @param pretty
* 是否美化
* @return 返回请求响应的HTML
*/
public static String doGetHttpHTML(String url, String cookie) {
StringBuffer response = new StringBuffer();
DefaultHttpClient client = new DefaultHttpClient();
HttpGet method = new HttpGet(url);
// 设置Http header数据
if(cookie != null){
String sessionId=StringUtils.cookieHandle(cookie.toString());
method.setHeader("Cookie",sessionId);
}
try {
HttpResponse res=client.execute(method);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity=res.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
String line;
while ((line = reader.readLine()) != null) {
response.append(line).append(System.getProperty("line.separator"));
}
reader.close();
}
return response.toString();
} catch(IOException e) {
Log.i("执行HTTP Get请求" + url + "时,发生异常!", e.toString());
return "InternetFail";
} finally {
client.getConnectionManager().shutdown();
}
}
以上获取返回值和httpurlconnection 功能差不多 ,一下这段代码获取cookie
/**
* 获取登陆Cookie
* @param urlpath
* @return
* @throws Exception
*/
public static String getData(String url){
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
try {
HttpResponse response = httpclient.execute(httpget);
CookieStore cookie=httpclient.getCookieStore();
return cookie.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}