8

이 코드를 사용하면 일부 레이아웃을 동적으로 쉽게 삽입 할 수 있습니다. 레이아웃에는 Button이 포함되어 있는데,이 레이아웃을 startActivityForResult으로 시작하고 싶습니다. 이제 결과 (텍스트)를 얻었을 때 Button에 설정하려고합니다.안드로이드는 onActivityResult에서 값을 받아서 Button으로 설정합니다.

btnAggiungiCampo.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     PopupMenu popup = new PopupMenu(this, btnAggiungiCampo); 
     popup.getMenuInflater().inflate(R.menu.menu_campi, popup.getMenu()); 
     popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
      public boolean onMenuItemClick(MenuItem item) { 
       View child = null; 
       if (item.getTitle().equals(getString(R.string.Text))) { 
        child = getLayoutInflater().inflate(R.layout.inflate_campo, null); 
        rlCampi.addView(child); 

        Button btnGeneraPSW = (Button) child.findViewById(R.id.imageButton3); 
           btnGeneraPSW.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View v) { 
             Intent inte = new Intent(this, Genera_password.class); 
             startActivityForResult(inte, REQ_CODE_ACT1); 
            } 
           }); 
       } 
      } 
     } 
    } 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == REQ_CODE_ACT1) { 

      // how can I set?? 

     } 
    } 
} 
+1

플래그를 onActivityResult로 설정 한 다음 onResume에보기를 추가하십시오. – Qamar

답변

2

ImageButton에는 텍스트를 설정할 수 없습니다. ImageButton에는이 방법이 없습니다. 대신 Button을 사용하거나 이미지가 중요한 경우 TextView가있는 ImageButton을 사용해야합니다.

mYourTextView.setText(retrievedText); 
2

귀하의 rlCampiViewGroup이고 당신이 rlCampi.addView(child)를 사용하여 아이를 추가 :

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <ImageButton 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:scaleType="centerInside" 
     android:id="@+id/yourImageButton" 
     android:src="@drawable/yourSource" 
    /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/yourTextView" 
    /> 
</LinearLayout> 

그리고 당신이 당신의 텍스트 뷰에 검색 할 텍스트를 설정합니다. View에 얼마나 많은 자녀가 있는지 찾을 수 있습니다 (rlCampi.getChildCount()).

지금 아래

ImageButton btnGeneraPSW = (ImageButton) child.findViewById(R.id.imageButton3); 
btnGeneraPSW.setTag(rlCampi.getChildCount()); 
btnGeneraPSW.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent intent = new Intent(this, Genera_password.class); 
     intent.putExtra("btnGeneraPSW_position", (int) v.getTa()); 
     startActivityForResult(intent, REQ_CODE_ACT1); 
    } 
}); 

으로 코드를 대체 그리고 당신은 결과를 설정할 때 완료 한 후

Intent data = new Intent(); 
data.putExtra("btnGeneraPSW_position", getIntent().getIntExtra("btnGeneraPSW_position", -1)); 
setResult(Activity.RESULT_OK, data); 

그리고 onActivityResult

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == REQ_CODE_ACT1) { 
      int btnPosition = data.getIntExtra("btnGeneraPSW_position", -1); 
      if(btnPosition != -1){ 
       View childView = rlCampi.getChildAt(btnPosition); 
       // now you have your childView and activity result data 
       // using childView find your view and change its text you have from activity result data 
      } 
     } 
    } 
} 
+1

ok,하지만 RESULT_OK에서 텍스트를 반환해야합니다 – user2847219

+0

예 u 결과는 –

+0

이어야하지만 작동하지 않습니다 : Intent intent = new Intent(); intent.putExtra ("btnGeneraPSW_position", tvPassword.getText(). toString()); setResult (RESULT_OK, intent); finish(); – user2847219

6

내부가 Genera_password 활동에서 이렇게 줄을 추가 모든 작업. 당신은 Genera_password 활동에 결과를 설정해야 현재 활동

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == REQ_CODE_ACT1) { 
      String requredText=data.getExtras().getString("text"); 
      button.setText(requredText); 
     } 
    } 
} 
+0

onActivityResult 후에 뷰가 생성되기 때문에 실패합니다 – jorgemf

1

의 onActivityResult를에서

Intent data=new Intent(); 
data.putExtra("text",requiredText); 
setResult(Activity.RESULT_OK,data); 
finish(); //to destroy Genera_password Activity 

. 이 같은 작업을 수행 할 수 있습니다
Intent intent = new Intent(); 
intent.putExtra("btnGeneraPSW_position", tvPassword.getText().toString()); 
setResult(RESULT_OK, intent); 
finish(); 

그런 다음이 같은 onActivityResult에서이 String 값을 얻을 수 있습니다 :

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == REQ_CODE_ACT1) { 
      String tvPassword = data.getExtras().getString("btnGeneraPSW_position"); 
      ((Button) child.findViewById(R.id.imageButton3)).setText(tvPassword); 
     } 
    } 
} 

행운을 빕니다.

1

onActivityResult은 뷰를 만들기 전에 호출됩니다. 버튼이 아직 종료되지 않았기 때문에 버튼에 아무 것도 설정할 수 없다는 뜻입니다. @ Qamar는 결과를 변수에 저장하고 onActivityResume에있는 변수를 체크하고 올바른 값을 버튼에 설정해야한다고 말했습니다.

Object result = null; 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == REQ_CODE_ACT1) { 
      result = data.get.... 
     } 
    } 
} 

@Override 
public void onResume() { 
    if (data != null) { 
      findViewById(id).setValue(result); 
      result = null; 
    } 

} 
관련 문제