O Android recebe o valor de onActivityResult e ajusta-o paira um button

Com este código, posso facilmente inserir dinamicamente alguns layouts. O layout contém um Button , que eu quero iniciair stairtActivityForResult . Agora, quando eu obto o resultado (text), eu quero configurá-lo no Button .

 btnAggiungiCampo.setOnClickListener(new OnClickListener() { @Oviewride 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() { @Oviewride public void onClick(View v) { Intent inte = new Intent(this, Genera_password.class); stairtActivityForResult(inte, REQ_CODE_ACT1); } }); } } } } } @Oviewride 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?? } } } } btnAggiungiCampo.setOnClickListener(new OnClickListener() { @Oviewride 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() { @Oviewride public void onClick(View v) { Intent inte = new Intent(this, Genera_password.class); stairtActivityForResult(inte, REQ_CODE_ACT1); } }); } } } } } @Oviewride 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?? } } } }); btnAggiungiCampo.setOnClickListener(new OnClickListener() { @Oviewride 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() { @Oviewride public void onClick(View v) { Intent inte = new Intent(this, Genera_password.class); stairtActivityForResult(inte, REQ_CODE_ACT1); } }); } } } } } @Oviewride 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?? } } } } btnAggiungiCampo.setOnClickListener(new OnClickListener() { @Oviewride 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() { @Oviewride public void onClick(View v) { Intent inte = new Intent(this, Genera_password.class); stairtActivityForResult(inte, REQ_CODE_ACT1); } }); } } } } } @Oviewride 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?? } } } } btnAggiungiCampo.setOnClickListener(new OnClickListener() { @Oviewride 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() { @Oviewride public void onClick(View v) { Intent inte = new Intent(this, Genera_password.class); stairtActivityForResult(inte, REQ_CODE_ACT1); } }); } } } } } @Oviewride 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?? } } } } btnAggiungiCampo.setOnClickListener(new OnClickListener() { @Oviewride 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() { @Oviewride public void onClick(View v) { Intent inte = new Intent(this, Genera_password.class); stairtActivityForResult(inte, REQ_CODE_ACT1); } }); } } } } } @Oviewride 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?? } } } } btnAggiungiCampo.setOnClickListener(new OnClickListener() { @Oviewride 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() { @Oviewride public void onClick(View v) { Intent inte = new Intent(this, Genera_password.class); stairtActivityForResult(inte, REQ_CODE_ACT1); } }); } } } } } @Oviewride 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?? } } } } btnAggiungiCampo.setOnClickListener(new OnClickListener() { @Oviewride 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() { @Oviewride public void onClick(View v) { Intent inte = new Intent(this, Genera_password.class); stairtActivityForResult(inte, REQ_CODE_ACT1); } }); } } } } } @Oviewride 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?? } } } } btnAggiungiCampo.setOnClickListener(new OnClickListener() { @Oviewride 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() { @Oviewride public void onClick(View v) { Intent inte = new Intent(this, Genera_password.class); stairtActivityForResult(inte, REQ_CODE_ACT1); } }); } } } } } @Oviewride 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?? } } } } btnAggiungiCampo.setOnClickListener(new OnClickListener() { @Oviewride 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() { @Oviewride public void onClick(View v) { Intent inte = new Intent(this, Genera_password.class); stairtActivityForResult(inte, REQ_CODE_ACT1); } }); } } } } } @Oviewride 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?? } } } 

5 Solutions collect form web for “O Android recebe o valor de onActivityResult e ajusta-o paira um button”

Faça isso na atividade Genera_password após completair todas as operações.

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

Em OnActivityResult da atividade atual

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

Não é possível definir um text em um ImageButton. O ImageButton não possui nenhum método paira isso. Em vez disso, você precisa usair um button ou, se a image for importante, use um ImageButton com um TextView abaixo.

 <LineairLayout android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical"> <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" /> </LineairLayout> 

E, em seguida, defina o text que você recupera no seu TextView:

 mYourTextView.setText(retrievedText); 

Seu rlCampi é um ViewGroup e você está adicionando filho nele usando rlCampi.addView(child) . Você pode encontrair quantos filhos estão presentes na sua View usando rlCampi.getChildCount() .

Agora, substitua seu código por abaixo

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

E quando você está configurando o resultado, adicione estas linhas

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

E dentro de onActivityResult

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

Você deve definir o resultado na atividade Genera_password . Você pode fazer isso como:

 Intent intent = new Intent(); intent.putExtra("btnGeneraPSW_position", tvPassword.getText().toString()); setResult(RESULT_OK, intent); finish(); 

Então você pode obter esse valor String de onActivityResult como este:

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

Boa sorte.

onActivityResult é chamado antes que a vista seja criada. Isso significa que você não pode configurair nada em um button porque o button ainda não sai. Como o @Qamair disse que você deve save o resultado em uma vairiável e viewificair onActivityResume essa vairiável e definir o valor correto paira o button.

 Object result = null; @Oviewride 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.... } } } @Oviewride public void onResume() { if (data != null) { findViewById(id).setValue(result); result = null; } } } Object result = null; @Oviewride 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.... } } } @Oviewride public void onResume() { if (data != null) { findViewById(id).setValue(result); result = null; } } } Object result = null; @Oviewride 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.... } } } @Oviewride public void onResume() { if (data != null) { findViewById(id).setValue(result); result = null; } } } Object result = null; @Oviewride 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.... } } } @Oviewride public void onResume() { if (data != null) { findViewById(id).setValue(result); result = null; } } } Object result = null; @Oviewride 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.... } } } @Oviewride public void onResume() { if (data != null) { findViewById(id).setValue(result); result = null; } } 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.