Activity的跳转

2018-10-22  本文已影响4人  Ello_Orld

一个Android通常由多个Activity组成,其中一个Activity是作为应用程序的入口。Activity启动其他的Activity通常有一下几种方法:

1.两个Activity最简单的跳转

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class); 
                startActivity(intent);
            }
        });
    }
}

2. 使用Bundle在两个Activity之间交换数据

两个Activity之间,用Intent携带数据,只需要将要携带的数据放入Intent即可。
Intent提供了多种方法来携带额外的数据,主要有以下:


举例:FirstActivity用于收集用户数据,提交到SecondActivity进行展示。

public class FirstActivity extends AppCompatActivity {

    private EditText etUserName;
    private EditText etPassword;
    private RadioGroup rgGender;
    private Button btnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etUserName = findViewById(R.id.et_user_name);
        etPassword = findViewById(R.id.et_password);
        rgGender = findViewById(R.id.radioGroup);
        btnSubmit = findViewById(R.id.btn_submit);


        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String gender = "女";
                if (rgGender.getCheckedRadioButtonId() == R.id.rb_male) {
                    gender = "男";
                }
                Person person = new Person(etUserName.getText().toString(), etPassword.getText().toString(), gender);
                Bundle b = new Bundle();
                b.putSerializable("person", person);
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtras(b);
                startActivity(intent);
            }
        });
    }
}
public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Bundle extras = getIntent().getExtras();
        ((TextView) findViewById(R.id.textView)).setText(((Person) extras.getSerializable("person")).toString());
    }
}

3. 启动Activity并返回结果

startActivityForResult(Intent intent, int resultCode)
这个方法用于启动指定的Activity并请求获取指定Activity的返回结果。


举例:
第一个Activity是一个简单的布局,包含一个按钮和文本显示框
点击按钮跳转到第二个Activity选择城市,返回结果显示在第一个Activity中。

第一个Acitivity:

public class MainActivity extends AppCompatActivity {

    private TextView result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
    }

    public void chooseCity(View view) {
        Intent intent = new Intent(MainActivity.this, SelectCityActivity.class);
        //1001是请求码
        startActivityForResult(intent, 1001);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1001 && resultCode == 1002) {
            Bundle bundle = data.getExtras();
            if (bundle != null) {
                result.setText(bundle.getString("city"));
            }
        }
    }
}

第二个Activity

public class SelectCityActivity extends AppCompatActivity {
    ...
    private static final String[] CITIES = {"北京", "上海", "广州"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
       ...
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.item_view, CITIES);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = getIntent();
                intent.putExtra("city", CITIES[position]);
                SelectCityActivity.this.setResult(1002, intent); //结果码
                finish();
            }
        });
    }
}

image.png

tobecontinued

上一篇下一篇

猜你喜欢

热点阅读