Activity组件

Android的四大组件:Activity,BroadcastReceiver,Service,ContentProvider

(一)Activity

1.效果图:点击开启第二个Activity 显示到第二个页面

    

2.布局: 两个布局

activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.app2.MainActivity"
11     android:orientation="vertical">
12 
13     <Button
14         android:id="@+id/btn_open"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:text="开启第二个 Activity " />
18 </LinearLayout>

slayout

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6     <TextView
 7         android:id="@+id/tv"
 8         android:textColor="@color/colorAccent"
 9         android:textSize="30dp"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content" />
12     <Button
13         android:id="@+id/btn_close"
14         android:text="关闭当前Activity"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content" />
17 
18 </LinearLayout>

3.注册

<activity android:name=".SActivity"></activity>

 4.

 MainActivity.java

 1 package com.example.app2;
 2 
 3 import android.content.Intent;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.Toast;
 9 
10 public class MainActivity extends AppCompatActivity {
11     private Button button;
12 
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activity_main);
17         button = (Button) findViewById(R.id.btn_open);
18         final Intent intent = new Intent(MainActivity.this,SActivity.class);
19         button.setOnClickListener(new View.OnClickListener() {
20             @Override
21             public void onClick(View v) {
22                 intent.putExtra("info","second Activity");
23                 startActivity(intent);
24             }
25         });
26     }
27 }

SActivity.java

 1 package com.example.app2;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.TextView;
 9 
10 /**
11  * Created by Administrator on 2018/5/22.
12  */
13 public class SActivity extends Activity {
14     private Button button_close;
15     private TextView textView;
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.slayout);
21         textView = (TextView)findViewById(R.id.tv);
22         button_close = (Button)findViewById(R.id.btn_close);
23         Intent intent = getIntent();
24         String info = intent.getStringExtra("info");
25         textView.setText(info);
26         button_close.setOnClickListener(new View.OnClickListener() {
27             @Override
28             public void onClick(View v) {
29 
30                 finish();
31 
32             }
33         });
34 
35     }
36 }

猜你喜欢

转载自www.cnblogs.com/sunxiaoyan/p/9071800.html