如何在android中将textView Value From One发送到其他?

时间:2014-11-16 14:18:40

标签: android string onbackpressed

我正在构建一个Android应用程序在点击图像视图时,会调用OnBackPressed函数。

这是代码 -

    ImageView imgv = (ImageView) mCustomView.findViewById(R.id.back);

    imgv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub

            onBackPressed();
        }
    });

现在问题是如何使用此OnBackPressed函数发送文本视图字符串。

1 个答案:

答案 0 :(得分:3)

如果要开始第二项活动,请使用以下代码:

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

如果您想返回第一个活动,请使用以下代码:

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();

最后在第一个活动中,您可以通过以下方法获得返回值:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if(resultCode == RESULT_OK) {
            String result = data.getStringExtra("result");
        }
        if (resultCode == RESULT_CANCELED) {
            // If there is no result 
        }
    }
}

示例:

因此,使用上面的代码来开发你所说的例子:

在forst活动中开始第二项活动:

public void ButtonClickedMethod(View v)
{
    Intent i = new Intent(this, Second.class);
    startActivityForResult(i, 1);
}

然后在第二个活动的on back press方法中使用此代码:

@Override
public void onBackPressed()
{
    Intent returnIntent = new Intent();
    returnIntent.putExtra("result", ((TextView) findViewById(R.id.text)).getText());
    setResult(RESULT_OK,returnIntent);
    finish();
}

再次对第一个活动使用此代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == 1) {
        if(resultCode == RESULT_OK) {
            String result = data.getStringExtra("result");
            ((Button) findViewById(R.id.button1)).setText(result);
        }
        if (resultCode == RESULT_CANCELED) {
            // If there is no result 
        }
    }
}
相关问题