Android開(kāi)發(fā)學(xué)習(xí)教程(22)- Intent傳值與startActivityForResult返回值
2023-01-28 14:55 作者:ChatGPT云炬學(xué)長(zhǎng) | 我要投稿
—— 你要悄悄拔尖,然后一鳴驚人。
上一篇我們講了Intent的基本概念,現(xiàn)在我們通過(guò)一個(gè)實(shí)例來(lái)驗(yàn)證。
Intent傳值
有這么一個(gè)場(chǎng)景,用戶在登錄頁(yè)面輸入用戶名之后點(diǎn)擊登錄,跳轉(zhuǎn)到另一個(gè)頁(yè)面顯示用戶名名稱。這個(gè)場(chǎng)景就是典型的Activity間Intent傳值。下面我們開(kāi)始編寫代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public
?class
?LoginActivity?
extends
?AppCompatActivity {
????
private
?EditText etUsername;
????
private
?Button btnLogin;
????
@Override
????
protected
?void
?onCreate(Bundle savedInstanceState) {
????????
super
.onCreate(savedInstanceState);
????????
setContentView(R.layout.activity_login);
????????
initView();
????
}
????
private
?void
?initView() {
????????
etUsername = findViewById(R.id.et_username);
????????
btnLogin = findViewById(R.id.btn_login);
????????
btnLogin.setOnClickListener(
new
?View.OnClickListener() {
????????????
@Override
????????????
public
?void
?onClick(View v) {
????????????????
Intent?intent?=?
new
?Intent(LoginActivity.
this
, MainActivity.
class
);
????????????????
intent.putExtra(
"username"
, etUsername.getText().toString());
????????????????
startActivity(intent);
????????????
}
????????
});
????
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public
?class
?MainActivity?
extends
?AppCompatActivity {
????
private
?TextView tvUsername;
????
@Override
????
protected
?void
?onCreate(Bundle savedInstanceState) {
????????
super
.onCreate(savedInstanceState);
????????
setContentView(R.layout.activity_main);
????????
initView();
????
}
????
private
?void
?initView() {
????????
tvUsername = findViewById(R.id.tv_username);
????????
tvUsername.setText(
"歡迎您 "
?+ getIntent().getStringExtra(
"username"
));
????
}
}
在登錄界面輸入用戶名之后,點(diǎn)擊登錄,通過(guò)startActivity跳轉(zhuǎn)到主頁(yè)面MainActivity,在MainActivity上通過(guò)getIntent()獲取Intent傳值。
Intent返回值給上一個(gè)Activity
另外還有這么一個(gè)場(chǎng)景,比如餓了么APP用戶在首頁(yè)選擇用戶所在城市,比如選擇廣州之后返回,首頁(yè)顯示廣州了。這個(gè)場(chǎng)景就是典型Intent返回值給上一個(gè)Activity。下面我們開(kāi)始編寫代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public
?class
?ChooseActivity?
extends
?AppCompatActivity {
????
private
?TextView tvCity;
????
@Override
????
protected
?void
?onCreate(Bundle savedInstanceState) {
????????
super
.onCreate(savedInstanceState);
????????
setContentView(R.layout.activity_choose);
????????
tvCity = findViewById(R.id.tv_city);
????????
tvCity.setOnClickListener(
new
?View.OnClickListener() {
????????????
@Override
????????????
public
?void
?onClick(View v) {
????????????????
startActivityForResult(
new
?Intent(ChooseActivity.
this
, CityListViewActivity.
class
),?
101
);
????????????
}
????????
});
????
}
????
@Override
????
protected
?void
?onActivityResult(
int
?requestCode,?
int
?resultCode,?
@Nullable
?Intent data) {
????????
super
.onActivityResult(requestCode, resultCode, data);
????????
if
?(requestCode ==?
101
?&& resultCode == RESULT_OK && data !=?
null
) {
????????????
tvCity.setText(data.getStringExtra(
"city"
));
????????
}
????
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public
?class
?CityListViewActivity?
extends
?AppCompatActivity {
????
@Override
????
protected
?void
?onCreate(Bundle savedInstanceState) {
????????
super
.onCreate(savedInstanceState);
????????
setContentView(R.layout.activity_city);
????????
ListView listView = findViewById(R.id.listview);
????????
String[] data = {
"北京"
,?
"上海"
,?
"廣東"
,?
"江西"
,?
"福建"
,?
"江蘇"
,?
"山東"
,?
"浙江"
,?
"四川"
,?
"云南"
,?
"貴州"
,?
"黑龍江"
,?
"哈爾濱"
};
????????
ArrayAdapter<String> array =?
new
?ArrayAdapter<>(
this
,?android.R.layout.simple_list_item_1, data);
????????
listView.setAdapter(array);
????????
listView.setOnItemClickListener(
new
?AdapterView.OnItemClickListener() {
????????????
@Override
????????????
public
?void
?onItemClick(AdapterView<?> parent, View view,?
int
?position,?
long
?id) {
????????????????
Intent intent =?
new
?Intent();
????????????????
intent.putExtra(
"city"
, data[position]);
????????????????
setResult(RESULT_OK, intent);
????????????????
finish();
????????????
}
????????
});
????
}
}
源碼鏈接:https://yunjunet.cn/876800.html
標(biāo)簽: