我在片段中添加了onClick()
按钮以显示结果。我还在onActivityResult()
中添加了MyActivity
方法。我想在更多片段中添加onClick()
按钮并显示结果。
在这种情况下,我应该如何在活动中编写多个onActivityResult()
方法?
以下是OnActivityResultmethod
的代码:
button = (Button) view.findViewById(R.id.zing);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
scanIntegrator.initiateScan();
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode,
data);
Fragment fragment = this.getFragmentManager().findFragmentById(R.id.total_frame_content);
if (fragment instanceof CustomerIdFragment) {
if (scanResult != null) {
if (scanResult.getContents() == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
} else {
//String customerSno = scanResult.getContents().substring(0, 10),
// passCode = scanResult.getContents().substring(10, 14);
Toast.makeText(this, " " + scanResult.getContents(), Toast.LENGTH_SHORT).show();
// System.out.println("Result" + scanResult.getContents());
}
}
}
}
答案 0 :(得分:1)
试试这个你不能多次覆盖方法,所以你的价值
startActivityForResult(intent,1);
in on activity result
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1){
if(resultCode == RESULT_OK){
//do somthing
}
}
if(requestCode == 2){
if(resultCode == RESULT_OK){
//do somthing
}
}
if(requestCode == 3){
if(resultCode == RESULT_OK){
//do somthing
}
}
}
已编辑的答案 在你的片段java文件
private int PICK_IMAGE_REQUEST_CODE = 1;
btChoseFile = (Button)view.findViewById(R.id.btChoseFile);
btStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST_CODE);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_REQUEST_CODE){
Toast.makeText(getActivity(),"On Activity Result in fragment",Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:0)
您只需要使用switch
案例,如下所示:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch (requestCode) {
case REQUEST_CODE_CAPTURE_IMAGE:
if (requestCode == REQUEST_CODE_CAPTURE_IMAGE && resultCode == Activity.RESULT_OK ) {
//your code
break;
case PHOTO_PICKER_ID:
if (requestCode == PHOTO_PICKER_ID && resultCode == Activity.RESULT_OK && null != data) {
//your code
}
break;
}
} catch (Exception e)
{
Log.d("krvrrusbviuritiribtr", e.getMessage());
}
}
答案 2 :(得分:0)
当您调用 startActivityForResult(intent,requestCode); 时,您只需要使用不同的requestCode。此号码将发送至 onActivityResult(requestCode,resultCode,intent)
为您的活动中的请求创建一些常量(称之为MyActivity)。
public class MyActivity{
private static final int REQUEST_ONE = 1;
private static final int REQUEST_TWO = 2;
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
//Management of the result, see below
}
}
使用与请求调用相对应的常量。
//startActivityForResult(intent,REQUEST_CODE);
在第一个片段中,您将使用
public void clickButton(View v){
startActivityForResult(intent,MyActivity.REQUEST_ONE);
}
在第二个片段中,您将使用
public void clickButton(View v){
startActivityForResult(intent,MyActivity.REQUEST_TWO);
}
然后,您只需要在activity中实现result方法,以处理requestCode值以执行正确的代码。使用开关或if else条件。有更多的可能性(使用某种模式),但最简单的是那些。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode == REQUEST_ONE){
// the code for request one
} else if(requestCode == REQUEST_TWO){
// the code for request two
} else {
super.onActivityResult(requestCode, resultCode, intent);
}
}
或使用开关
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch(requestCode)
case REQUEST_ONE:
// the code for request one
break;
case REQUEST_TWO:
// the code for request two
break;
default:
super.onActivityResult(requestCode, resultCode, intent);
}
}
如果您有太多请求,请为每个请求创建一个方法,这将使结果方法更具可读性。