91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Activity 之間的交互

發布時間:2020-06-01 10:07:11 來源:網絡 閱讀:489 作者:just2012xia 欄目:移動開發

在第一個activity中使用

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


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

阜宁县| 诸暨市| 吉水县| 额济纳旗| 武陟县| 巧家县| 贞丰县| 固原市| 通渭县| 双牌县| 应用必备| 美姑县| 武夷山市| 金秀| 胶南市| 班玛县| 太原市| 佛山市| 扶风县| 右玉县| 勃利县| 海阳市| 广东省| 天柱县| 谢通门县| 中宁县| 宜兰市| 大安市| 镇康县| 新宁县| 毕节市| 邵阳市| 拜泉县| 屏东市| 田东县| 淮阳县| 抚顺县| 城步| 郑州市| 东丽区| 墨脱县|