【问题标题】:Android receive value from onActivityResult and set it to a ButtonAndroid 从 onActivityResult 接收值并将其设置为 Button
【发布时间】:2026-02-10 17:05:01
【问题描述】:

使用此代码,我可以轻松地动态插入一些布局。布局包含一个Button,我想启动它startActivityForResult。现在,当我得到结果(文本)时,我想将其设置在 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??

        }
    }
}

【问题讨论】:

  • 设置标志 onActivityResult 然后在 onResume 中添加你的视图。

标签: android android-activity android-button startactivityforresult


【解决方案1】:

完成所有操作后,在 Genera_password Activity 中执行此操作。

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

在当前活动的 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) {
            String requredText=data.getExtras().getString("text");
            button.setText(requredText);
        }
    }
}

【讨论】:

  • 这会失败,因为视图是在onActivityResult之后创建的
【解决方案2】:

您不能在 ImageButton 上设置文本。 ImageButton 对此没有任何方法。相反,您必须使用 Button,或者,如果图像很重要,请使用 ImageButton,下方带有 TextView。

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

然后将您检索到的文本设置到您的 TextView:

mYourTextView.setText(retrievedText);

【讨论】:

    【解决方案3】:

    您的rlCampiViewGroup,您正在使用rlCampi.addView(child) 在其中添加子项。您可以使用rlCampi.getChildCount() 查找您的View 中有多少孩子。

    现在用下面的代码替换你的代码

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

    【讨论】:

    • 是的,你应该是
    • 我的设置结果但不起作用:Intent intent = new Intent(); intent.putExtra("btnGeneraPSW_position", tvPassword.getText().toString()); setResult(RESULT_OK, 意图);完成();
    • 你做得不好,你必须按照我告诉你的去做。你应该intent.putExtra("btnGeneraPSW_position", (int) v.getTag()) 而不是intent.putExtra("btnGeneraPSW_position", tvPassword.getText().toString())
    • 你的回复我得到“null”
    【解决方案4】:

    您应该在Genera_password 活动中设置结果。你可以这样做:

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

    祝你好运。

    【讨论】:

      【解决方案5】:

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

      【讨论】: