2016-05-12 53 views
0

我在片段中添加了onClick()按鈕以顯示結果。我還在MyActivity中加入了onActivityResult()方法。我想在更多片段中添加onClick()按鈕並顯示結果。Android中的多重OnactivityResult方法

在這種情況下,我應該如何在活動中編寫多個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()); 

        } 
       } 

      } 
     } 

回答

1

嘗試這個你無法在此改變的方法多的時間,這樣你們PAS int value

startActivityForResult(intent,1); 

在活動結果

@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(); 
     } 
    } 
+0

android geeks ...我是初學者到android..where我應該調用startActivityForResult(intent,1);和我的Onclik代碼片段.... 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(); } });像這樣 – Kingraj

+0

@Kingraj對不起,我不明白你想做什麼? –

+0

如果你想開始活動的結果u也可以從你的按鈕調用它,你也可以overActivityResult在你的片段 –

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()); 
      } 
     } 
0

當你調用startActivityForResult(意向,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); 
} 

然後你只需要實現在活動結果的方法請注意requestCode值以執行正確的代碼。使用開關或其他條件。有更多的可能性(使用某種模式),但最簡單的是那些。

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

如果你有太多的要求,爲每個請求的一個方法,這將讓結果的方法更具有可讀性。

+0

即使我的回答與其他人相似,也沒有人會解釋如何設置requestCode,因此我決定糾正該問題。 – AxelH

+0

好的兄弟..沒有必要寫任何碎片哈? – Kingraj

+0

和我應該在哪裏調用startActivityForResult(intent,REQUEST_ONE); – Kingraj