安卓界面跳转
intent界面跳转与传数据
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
27
28
29
30
31
32
33
34
35
36package com.example.python.myapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class All_single_Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.layout4 );
//接受数据
Intent intent4 = getIntent();
String name = (String)intent4.getSerializableExtra( "heroName" );
Hero hero = new Hero(name);
TextView info = (TextView)findViewById( R.id.all_hero_info );
String story = hero.getHeroStory(name);
info.setText( story );
//返回逻辑上一页
Button btn = (Button)findViewById( R.id.all_back_home );
btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent( All_single_Activity.this,DetailActivity.class );
All_single_Activity.this.startActivity( intent );
}
} );
}
}listview与监听
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58package com.example.python.myapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class DetailActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
ListView listView = (ListView)findViewById(R.id.list_item);
//适配器
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,hero));
//监听
listView.setOnItemClickListener(this);
//返回主页
Button back_home = (Button)findViewById(R.id.all_back_home);
back_home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(DetailActivity.this,MainActivity.class);
DetailActivity.this.startActivity(intent);
}
});
}
private static final String[] hero = new String[]{
"猎空",
"小美",
"温斯顿",
"D.VA",
"布丽吉塔"
};
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView item = (ListView)findViewById( R.id.list_item );
String str = item.getItemAtPosition( position ).toString();
// Toast.makeText(DetailActivity.this,str,Toast.LENGTH_LONG).show();
Intent intent = new Intent( DetailActivity.this,All_single_Activity.class );
Bundle mExtra = new Bundle();
mExtra.putSerializable( "heroName",str );
intent.putExtras( mExtra );
DetailActivity.this.startActivity( intent );
}
}