startactivityforresult
-
从子Activity获取返回结果
public void startActivityForResult(intent intent, int requestcode)
该方法的第一个参数是intent,第二个参数是请求代码。请求代码先发送给子activity,然后再返回给父activity的用户定义整数值。当一个activity启动多个不同类型的子activity,且需要判断区分消息回馈方时,通常会用到该请求代码
-
设置返回结果
public final void setResult(int resultCode)
public final void setResult(int resultCode,Intent data)
一般来说,参数resultCode可以是以下两个预定义常量中的任何一个
Activity.RESULT_OK
Activity.RESULT_CANCELED
-
一个例子的效果图
-
Demo目录
-
字符串资源
<string name="one">1+1=?</string>
<string name="two">2+2=?</string>
<string name="one_answer">2</string>
<string name="two_answer">4</string>
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<Linearlayout
xmlns:Android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/one"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/two"/>
</LinearLayout>
- MainActivity.java
public class MainActivity extends APPCompatActivity {
private static final int REQUEST_ONE = 1;
private static final int REQUEST_TWO = 2;
private Button btn1;
private Button btn2;
@Override
protected void onCreate(Bundle savedinstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(ChooseActivity.newIntent(MainActivity.this), REQUEST_ONE);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(ChooseActivity.newIntent(MainActivity.this), REQUEST_TWO);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
showResult(requestCode,ChooseActivity.getResultAnswer(data));
}
}
private void showResult(int requestCode, int answer) {
String str;
if (requestCode == answer) {
str = "正确";
} else {
str = "错误";
}
snackbar.make(this.getWindow().getDecorView(), str, Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
}
- activity_choose.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_choose"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/one_answer"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/two_answer"/>
</LinearLayout>
- ChooseActivity.java
public class ChooseActivity extends AppCompatActivity {
public static final String RESULT_ANSWER = "result_answer";
public static Intent newIntent(context context) {
return new Intent(context, ChooseActivity.class);
}
public static int getResultAnswer(Intent intent) {
return intent.getIntExtra(RESULT_ANSWER, 0);
}
private Button btn1;
private Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.putExtra(RESULT_ANSWER, 1);
setResult(RESULT_OK, i);
finish();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.putExtra(RESULT_ANSWER, 2);
setResult(RESULT_OK, i);
finish();
}
});
}
}
- 如果要使用Snackbar,要加入依赖
compile 'com.android.support:design:24.0.0-beta1'
文章最后发布于: 2016-12-02 00:07:06
相关阅读
转自:http://blog.chinaunix.net/uid-25785357-id-3434344.html使用crontab你可以在指定的时间执行一个shell脚本或者一系列Linux
consider的用法consider大致有两种含义。含义不同,用法有别。现归纳如下。一.consider作“考虑”解,常用于以下句型:1. Consider+名词
本文主要介绍C++中的string类的常见用法。 1. 概述 string是C++标准库的一个重要的部分,主要用于字符串处理。可以使用输入输出流
提升进程权限-OpenProcessToken等函数的用法 GetCurrentProcessID 得到当前进程的ID OpenProcessToken 得到进程的令牌句柄 Lo
浅谈setInterval(aa,1000)与setInterval(aa(),1000)的
一直有个疑惑,在定时器上调用某个方法时,加括号和不加括号有什么区别。今天做了个实验,发现,不加括号定时器会每秒执行一次,加了括号只