1. 程式人生 > 其它 >Android學習——Activity、Bundle

Android學習——Activity、Bundle

1、手動建立Activity

  學習這個的目的是明白activity建立的每一個步驟,在實際開發的時候直接建立一個activity+main.java檔案即可

  步驟:①建立繼承自Activity的Activity;②重寫回調方法onCreate();③設定要顯示的檢視 setContentView(R.layout.activity_main);

  注:需要在ActivityMainifest.xml配置<activity android:name="" />

  activity通過Intent表達“意圖”

  關閉activity:finish();重新整理:onCreate(null);

  通過activity進行頁面跳轉

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<TextView
    android:layout_width="wrap_content"
android:layout_height="wrap_content" android:paddingTop="20dp" android:id="@+id/wang_mima" android:textColor="#ff4500" android:text="忘記密碼" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageButton android:layout_width="15dp" android:layout_height="15dp" android:id="@+id/close" android:src="@drawable/close" android:background="#0000" android:layout_marginTop="15dp" android:scaleType="fitXY"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請輸入新密碼"/> <EditText android:layout_width="250dp" android:layout_height="wrap_content" android:hint="新密碼"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交"/> </LinearLayout>
package com.example.learning;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = findViewById(R.id.wang_mima);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,PasswordActivity.class);
                startActivity(intent);
            }
        });
    }
}
package com.example.learning;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import androidx.annotation.Nullable;

public class PasswordActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        ImageButton imageButton = findViewById(R.id.close);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}

2、通過Bundle儲存資料

  Bundle是鍵值對的組合

 

 直接用例子進行說明,返回提交的資料,效果是這樣的

 

 程式碼:

package com.example.addinfromation;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String user = ((EditText)findViewById(R.id.user)).getText().toString();
                String password = ((EditText)findViewById(R.id.password)).getText().toString();
                if(!"".equals(user)&&!"".equals(password)){
                    Intent intent = new Intent(MainActivity.this,InformationActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putCharSequence("user",user);
                    bundle.putCharSequence("password",password);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else {
                    Toast.makeText(MainActivity.this,"請將資訊填寫完整",Toast.LENGTH_LONG);
                }
            }
        });
    }
}
package com.example.addinfromation;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class InformationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information);
        Intent intent=getIntent();
        Bundle bundle = intent.getExtras();
        String user = bundle.getString("user");
        String password = bundle.getString("password");
        TextView tv_user = findViewById(R.id.user);
        TextView tv_password = findViewById(R.id.password);
        tv_user.setText(user);
        tv_password.setText(password);
    }
}