在一個(gè) Activity 中改變另一個(gè) Activity 的 UI

在一個(gè) Activity 中更改另一個(gè) Activity 的 UI 可以讓兩個(gè) Activity 共享一個(gè) Handler 達(dá)到其效果,這里介紹的是另一種方法。
使用場(chǎng)景是點(diǎn)擊主界面的按鈕進(jìn)入第二個(gè)界面,在第二個(gè)頁(yè)面進(jìn)行相關(guān)操作返回后,主界面的 UI 或數(shù)據(jù)會(huì)發(fā)生改變。

主要用到的方法是 startActivityForResult

Demo
Demo

第一個(gè)頁(yè)面的設(shè)置

1. 在主布局文件中創(chuàng)建一個(gè) Button(用于進(jìn)入第二個(gè) Activity),一個(gè) TextView (用于在啟動(dòng)第二個(gè) Activity 后顯示改變 Text)
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="點(diǎn)擊進(jìn)入另一個(gè) Activity "
            android:id="@+id/botton_aty"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:id="@+id/textView" />
2. 為 Button 設(shè)置點(diǎn)擊事件
 button = (Button) findViewById(R.id.botton_aty);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.botton_aty:
                        Intent intent = new Intent(MainActivity.this,AnotherAty.class);
                        requesdCode = 0;    //需提前定義一個(gè):‘private int requesdCode;’
                        startActivityForResult(intent,requesdCode);    //這里用 startActivityForResult 的主要原因就是它可以回傳數(shù)據(jù)。
                }
            }
        });

第二個(gè)頁(yè)面的設(shè)置

1. 設(shè)置另一個(gè) Activity 的布局文件
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改變第一個(gè) Activity 中 TextView 的值"
        android:id="@+id/button"/>
2.設(shè)置另一個(gè) Activity 的 Java 文件

public class AnotherAty extends AppCompatActivity {
    private int requesdCode = 0;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another_aty);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(requesdCode,intent);
                finish();
            }
        });
    }
}
3. 設(shè)置回調(diào)方法
//從第二個(gè)頁(yè)面回來(lái)的時(shí)候會(huì)執(zhí)行 onActivityResult 這個(gè)方法 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 0:
                textView.setText("發(fā)生變化");
                textView.setTextSize(50);
        }
    }

歡迎關(guān)注我的博客、簡(jiǎn)書、CSDNGitHub

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評(píng)論 25 708
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,390評(píng)論 0 17
  • 安離開(kāi)的那一天,陽(yáng)光明媚,正值盛夏,候機(jī)廳里卻冷氣十足,不由得讓人打個(gè)寒顫,我站在她身后,看著她略微偏瘦的身體拖著...
    弋草閱讀 272評(píng)論 0 1
  • 在今天這么特殊的日子里,必須寫點(diǎn)什么。 今天是大兒子八歲的生日,同時(shí)也是把二寶送給姥姥姥爺家長(zhǎng)住的第一天。 ...
    悠游心晴閱讀 308評(píng)論 0 1
  • 心被掏空 傳來(lái)昨日晚風(fēng) 睡得那么朦朧 星光被移送 視頻上傳 在還沒(méi)被網(wǎng)絡(luò)限制的空間 刪除一些謠言 欠下的話費(fèi)把11...
    江城妖怪閱讀 221評(píng)論 0 0

友情鏈接更多精彩內(nèi)容