快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Activity之间的交互

在第一个activity中使用

创新互联是一家网站设计公司,集创意、互联网应用、软件技术为一体的创意网站建设服务商,主营产品:成都响应式网站建设品牌网站制作营销型网站。我们专注企业品牌在网站中的整体树立,网络互动的体验,以及在手机等移动端的优质呈现。网站设计制作、成都网站制作、移动互联产品、网络运营、VI设计、云产品.运维为核心业务。为用户提供一站式解决方案,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏析悦目的作品,网站的价值服务。

startActivityForResult(intent, state_tag);

启动第二个activity,在第二个activity中调用 setResult(,)

在第一个activity中重载onActivityResult()

第一个Activity:

public class MainActivity extends Activity {
    private TextView tv = null;
    private int state_tag = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView)findViewById(R.id.textView);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
                                
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("content", tv.getText().toString());
                i.putExtra("bundle", bundle);
                startActivityForResult(i, state_tag);
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == state_tag)
        {
            if(resultCode == RESULT_OK)
            {
                                    
                Log.i("MainActivity", data.getBundleExtra("bundle2").getString("newContend"));
                tv.setText(data.getBundleExtra("bundle2").getString("newContend"));
            }
            else
            {
                Log.i("MainActivity", "in else:");
                return;
            }
        }
    }
}

第二个activity:

package com.example.intenttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SecondActivity extends Activity {
    private EditText editText = null;
    private Button okButton = null;
    private Button cancelButton = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
          
        editText = (EditText)findViewById(R.id.editText);
        okButton = (Button)findViewById(R.id.okButton);
        cancelButton = (Button)findViewById(R.id.cancelButton);
        Intent i = getIntent();
        editText.setText(i.getBundleExtra("bundle").getString("content"));
        okButton.setOnClickListener(new View.OnClickListener() {
              
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(SecondActivity.this, MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("newContent", String.valueOf(editText.getText()));
                i.putExtra("bundle2", bundle);
                setResult(RESULT_OK, i);
                finish();
            }
        });
        cancelButton.setOnClickListener(new View.OnClickListener() {
              
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                setResult(RESULT_CANCELED);
                finish();
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
    }
}

文章标题:Activity之间的交互
网站地址:http://6mz.cn/article/gepsde.html

其他资讯