gpt4 book ai didi

java - Android 中的多个 Onactivityresult 方法

转载 作者:行者123 更新时间:2023-12-02 03:38:08 27 4
gpt4 key购买 nike

我在 fragment 中添加了 onClick() 按钮来显示结果。我还在 MyActivity 中添加了 onActivityResult() 方法。我想在更多 fragment 中添加 onClick() 按钮并显示结果。

在这种情况下,我应该如何在 Activity 中编写多个 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());

}
}

}
}

最佳答案

尝试这个,你不能多次覆盖方法,所以你可以使用 int 值

  startActivityForResult(intent,1);

Activity 结果

  @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
}

}

}

编辑答案在您的 fragment 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();
}
}

关于java - Android 中的多个 Onactivityresult 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37187189/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com