zoukankan      html  css  js  c++  java
  • Activity跳转及数据传递

    android studio中先加载android pacleable插件

    BaseActivity.java

    package com.chuanxidemo.shaoxin.demo04;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    
    /**
     * Created by shaoxin on 2017/2/21.
     */
    
    public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
    
        
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView();
            init();
            setListener();
        }
    
        protected abstract void setContentView();
    
        protected abstract void setListener();
    
        protected abstract void init();
    
    }

    Student.java(在这里直接右键选择Generate->选择parcleable自动生成代码,进一步自动生成get()set())

    package com.chuanxidemo.shaoxin.demo04;
    
    import android.os.Parcel;
    import android.os.Parcelable;
    
    /**
     * Created by shaoxin on 2017/2/22.
     */
    
    public class Student implements Parcelable {
    
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.name);
        }
    
        public Student() {
        }
    
        protected Student(Parcel in) {
            this.name = in.readString();
        }
    
        public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
            @Override
            public Student createFromParcel(Parcel source) {
                return new Student(source);
            }
    
            @Override
            public Student[] newArray(int size) {
                return new Student[size];
            }
        };
    }

    MainActivity.java

    package com.chuanxidemo.shaoxin.demo04;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends BaseActivity {
        private EditText edit;
        private Button startBtn;
    
    
        @Override
        protected void setContentView() {
            setContentView(R.layout.activity_main);
        }
    
        @Override
        protected void setListener() {
            startBtn.setOnClickListener(this);
        }
    
        @Override
        protected void init() {
            edit = (EditText) findViewById(R.id.edit);
            startBtn = (Button) findViewById(R.id.start_btn);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.start_btn:
                    Intent intent = new Intent(this,Main.class);
                    Student student = new Student();
                    student.setName(edit.getText().toString());
                    Bundle data = new Bundle();
                    data.putParcelable("student", student);
                    intent.putExtras(data);
                    startActivity(intent);
                    break;
            }
        }
    }

    Main.java

    package com.chuanxidemo.shaoxin.demo04;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    
    /**
     * Created by shaoxin on 2017/2/22.
     */
    public class Main extends BaseActivity{
        private TextView txt;
    
    
        @Override
        protected void setContentView() {
            setContentView(R.layout.main);
        }
    
        @Override
        protected void setListener() {
    
        }
    
        @Override
        protected void init() {
            txt = (TextView) findViewById(R.id.txt);
            Intent intent = getIntent();
            Bundle bundle = intent.getExtras();
            Student student = (Student) bundle.getParcelable("student");
            txt.setText(student.getName());
        }
    
        @Override
        public void onClick(View v) {
    
        }
    
      }

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.chuanxidemo.shaoxin.demo04.MainActivity">
    
        <EditText
            android:id="@+id/edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入内容" />
    
        <Button
            android:id="@+id/start_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转" />
    </LinearLayout>

    main.xml

    <?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:id="@+id/txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
  • 相关阅读:
    前端小白之每天学习记录php(2)数据库操作软件
    前端小白之每天学习记录简单的原生js路由
    Dualembedded LSTM for QA match: 双embedding的LSTM聊天匹配式模型
    歌词爬虫
    Web第六周作业:css单位
    一篇无厘头的Roadmap
    web第二周作业:Register Form
    Web第六周作业:position.fixed vs sticky
    Web第六周作业:css颜色表
    Web第三周作业:grid layout
  • 原文地址:https://www.cnblogs.com/ShaoXin/p/6429187.html
Copyright © 2011-2022 走看看