Android第六讲--Intent
显示启动新的活动
Intent intent=new Intent(MyActivity.this, MyOtherActivity.class);
startActivity(intent);
隐式的intent和延时的运行时绑定
if(somethingWeird && itDontLookGood){
Intent intent=new Intent(Intent.ACTION_DIAL,
Uri.parse(“tel:555-3483”));
startActivity(intent);
}
Linkify。匹配一定RegEx模式的文本都将会被转换为一个可以单击的超链接。
//单击 textView时就会打开浏览器或者电子邮件服务程序
Linkify.addLinks(textView, Linkify.WEB_urls|Linkify.EMAIL_ADDRESSES);
也可以在xml文件中指定
<TextView android:autoLink=”phone|email”/>
当然也可以定义自己的Linkify字符串,定义新的RegEx模式。Linkify还支持TransformFilter和MatchFilter接口。
子活动向父活动返回结果
启动一个子活动
private static fianl int SHOW_SUBACTIVITY=1; //SHOW_SUBACTIVITY是个request码,用来唯一标识返回了结果的子活动
Intent intetn=new Intent(this,MyOtherActivity.class);
startActivityForResult(intent, SHOW_SUBACTIVITY);
启动一个子活动,选择一上联系人
private static fianl int PICK_CONTACT_SUBACTIVITY=2;
Uri uri=Uri.parse(“content://contacts/people”);
Intent intent=new Intent(Intent.ACTION_PICK,uri);
startActivityForResult(intent, PICK_CONTACT_SUBACTIVITY);
子活动在调用finish之前,调用setResult向父活动返回结果。
Button okButton=(Button)findViewById(R.id.ok_button);
okButton.setOnclickListener(new
View.OnClickListener(){
public void onClick(View view){
Uri data=Uri.parse(“content://horses/”+selected_horse_id);
Intent result=new Intent(null,data);
result.putExtra(IS_INPUT_CORRECT,inputCorrect);
result.putExtra(SELECTED_PISTOL,selectedPistol);
setResult(RESULT_OK,result);
finish();
}
});
Button cancelButton=(Button)findViewById(R.id.cancel_button);
cancelButton.setOnclickListener(new
View.OnClickListener(){
public void onClick(View view){
setResult(RESULT_CANCELED,null);
finish();
}
});
父活动处理子活动返回的结果
子活动关闭时,会触发父活动的onActivityResult事件处理程序。
private static fianl int SHOW_SUBACTIVITY_ONE=1;
private static fianl int SHOW_SUBACTIVITY_TWO=2;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
switch(
requestCode){
case (SHOW_SUBACTIVITY_ONE):{
if( resultCode==Activity. RESULT_OK){
Uri horse=data.getData();
boolean inputCorrect=data.getBooleanExtra( IS_INPUT_CORRECT,false);
String selectedPistol=data.getStringExtra( SELECTED_PISTOL);
}
break;
}
case (SHOW_SUBACTIVITY_TWO):{
if(resultCode==Activity. RESULT_OK){
//TODO:Handle OK click
}
}
}
}
本文来自博客园,作者:高性能golang,转载请注明原文链接:https://www.cnblogs.com/zhangchaoyang/articles/1959283.html