Android从onActivityResult接收值并将其设置为Button

时间:2016-12-12 17:49:43

标签: android android-activity android-button startactivityforresult

使用此代码,我可以轻松地动态插入一些布局。该布局包含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??

        }
    }
}

5 个答案:

答案 0 :(得分:8)

完成所有操作后,在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);
        }
    }
}

答案 1 :(得分:2)

您无法在ImageButton上设置文本。 ImageButton没有这方面的方法。相反,您必须使用Button,或者,如果图像很重要,请使用带有TextView的ImageButton。

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

答案 2 :(得分:2)

您的rlCampiViewGroup,您正在使用rlCampi.addView(child)在其中添加子项。您可以使用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 
            }
        }
    }
}

答案 3 :(得分:1)

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

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

然后,您可以从String获取此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 tvPassword = data.getExtras().getString("btnGeneraPSW_position");
            ((Button) child.findViewById(R.id.imageButton3)).setText(tvPassword);
        }
    }
}
祝你好运。

答案 4 :(得分: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;
    }

}