简体   繁体   English

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

[英]Android receive value from onActivityResult and set it to a Button

With this code, I can easily insert dynamically some layouts. 使用此代码,我可以轻松地动态插入一些布局。 The layout contains a Button , which I want to launch startActivityForResult . 布局包含一个Button ,我想启动startActivityForResult Now when I get the result (text), I want to set it on the Button . 现在,当我得到结果(文本)时,我想在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??

        }
    }
}

Do this in Genera_password Activity after completing all the operations. 完成所有操作后,在Genera_password Activity中执行此操作。

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

In OnActivityResult of the current 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);
        }
    }
}

You cannot set a text on an ImageButton. 您无法在ImageButton上设置文本。 ImageButton has no method for this. ImageButton没有这方面的方法。 Instead you have to use a Button, or, if the image is important, use an ImageButton with a TextView beneath. 相反,您必须使用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>

And then set the text you retrieve to your TextView: 然后将检索到的文本设置为TextView:

mYourTextView.setText(retrievedText);

Your rlCampi is a ViewGroup and you are adding child in it using rlCampi.addView(child) . 您的rlCampi是一个ViewGroup ,您正在使用rlCampi.addView(child)在其中添加child。 You can find how many child's are present in your View using rlCampi.getChildCount() . 您可以使用rlCampi.getChildCount() View有多少个孩子。

Now replace your code with below 现在用下面的代码替换你的代码

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

And when you are setting result add these lines 当你设置结果时添加这些行

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

And inside onActivityResult 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 
            }
        }
    }
}

You should set result in Genera_password activity. 您应该在Genera_password活动中设置结果。 You can do this like: 你可以这样做:

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

Then you can get this String value from onActivityResult like this: 然后你可以从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);
        }
    }
}

Good luck. 祝好运。

onActivityResult is called before the view is created. 在创建视图之前调用onActivityResult It means you cannot set anything to a button because the button doesn't exits yet. 这意味着您无法为按钮设置任何内容,因为该按钮尚未退出。 As @Qamar said you have to save the result into a variable and the check in onActivityResume that variable and set the correct value to the button. 正如@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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM