赞
踩
Android通过继承Application类,实现全局变量的存储更新与取出,可以解决用户登录状态,以及一些每个活动间都需要传递的变量。以及解决从Activity到Fragment,有Fragment到Activity的值得一系列传递。
效果图:
package com.example.globalvariableuse; import android.app.Application; import android.content.Context; public class MyApplication extends Application { private static Context context; private static MyApplication instance; //新建静态变量 private static String Globalvariable; @Override public void onCreate() { super.onCreate(); //获取去全局context context = getApplicationContext(); //实例个人Application类 instance = this ; } public static MyApplication getInstance() { return instance; } public static void setInstance(MyApplication instance) { MyApplication.instance = instance; } public static Context getContext() { return context; } public static void setContext(Context context) { MyApplication.context = context; } public static String getGlobalvariable() { return Globalvariable; } public static void setGlobalvariable(String globalvariable) { //这一步很重要 MyApplication.Globalvariable = globalvariable; } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.globalvariableuse"> <application //添加注册应用,以便初始化自己的Application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Main2Activity" /> <activity android:name=".Main3Activity"></activity> </application> </manifest>
修改activity_main.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button_send_activity2" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置一个页面2的跳转"/> <Button android:id="@+id/button_send_activity3" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:text="设置一个页面3的跳转"/> </LinearLayout> <EditText android:id="@+id/edit_input" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button_takeout" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取出输入的值,并赋给全局变量"/> <TextView android:id="@+id/text_response" android:layout_width="wrap_content" android:layout_marginTop="100dp" android:textStyle="bold" android:textColor="#F00" android:layout_height="wrap_content" /> </LinearLayout>
修改activity_main2.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".Main2Activity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button_send_activity1" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置一个页面1的跳转"/> <Button android:id="@+id/button_send_activity3" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:text="设置一个页面3的跳转"/> </LinearLayout> <Button android:id="@+id/button_takeout" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取出全局变量值"/> <TextView android:id="@+id/text_response" android:layout_width="wrap_content" android:layout_marginTop="100dp" android:textStyle="bold" android:textColor="#F00" android:layout_height="wrap_content" /> </LinearLayout>
修改activity_main3.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".Main3Activity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button_send_activity1" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置一个页面1的跳转"/> <Button android:id="@+id/button_send_activity2" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:text="设置一个页面2的跳转"/> </LinearLayout> <EditText android:id="@+id/edit_input" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button_takeout" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取出全局变量值"/> <TextView android:id="@+id/text_response" android:layout_width="wrap_content" android:layout_marginTop="100dp" android:textStyle="bold" android:textColor="#F00" android:layout_height="wrap_content" /> </LinearLayout>
修改MainActivity.java
package com.example.globalvariableuse; 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.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button button1,button2,button3; private TextView textView; private EditText editText; //新建变量 String variable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) findViewById(R.id.button_send_activity2); button2 = (Button) findViewById(R.id.button_send_activity3); button3 = (Button) findViewById(R.id.button_takeout); textView = (TextView) findViewById(R.id.text_response); editText = (EditText) findViewById(R.id.edit_input); } @Override protected void onStart() { super.onStart(); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); System.out.println("是否传入值"); System.out.println(variable); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.button_send_activity2: Intent Twointent = new Intent(MainActivity.this,Main2Activity.class); startActivity(Twointent); break; case R.id.button_send_activity3: Intent Threeintent = new Intent(MainActivity.this,Main3Activity.class); startActivity(Threeintent); break; case R.id.button_takeout: //赋variable值为我们取出来的editText的输入值 variable = editText.getText().toString(); //进行是否为空判断,虽然初始值为空,写这个if条件判断, // 是为了活动1和活动3,可以根据输入值是否为空, // 来进行判断,进行简单修改全局变量的值 if (!"".equals(variable)){ //将输入的值赋给全局变量variable MyApplication.setGlobalvariable(variable); } //通过textView显示我们赋的值 textView.setText("将全局变量取出:"+MyApplication.getGlobalvariable()); System.out.println("是否获取到输入值"+MyApplication.getGlobalvariable()); break; default: } } }
修改Main2Activity.java
package com.example.globalvariableuse; 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.EditText; import android.widget.TextView; public class Main2Activity extends AppCompatActivity implements View.OnClickListener { private Button button1,button2,button3; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); button1 = (Button) findViewById(R.id.button_send_activity1); button2 = (Button) findViewById(R.id.button_send_activity3); button3 = (Button) findViewById(R.id.button_takeout); textView = (TextView) findViewById(R.id.text_response); } @Override protected void onStart() { super.onStart(); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.button_send_activity1: Intent Twointent = new Intent(this,MainActivity.class); startActivity(Twointent); break; case R.id.button_send_activity3: Intent Threeintent = new Intent(this,Main3Activity.class); startActivity(Threeintent); break; case R.id.button_takeout: textView.setText("将全局变量取出:"+MyApplication.getGlobalvariable()); break; default: } } }
修改Main3Activity.java布局文件
package com.example.globalvariableuse; 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.EditText; import android.widget.TextView; public class Main3Activity extends AppCompatActivity implements View.OnClickListener { private Button button1,button2,button3; private TextView textView; private EditText editText; //新建变量 String variable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); button1 = (Button) findViewById(R.id.button_send_activity1); button2 = (Button) findViewById(R.id.button_send_activity2); button3 = (Button) findViewById(R.id.button_takeout); textView = (TextView) findViewById(R.id.text_response); editText = (EditText) findViewById(R.id.edit_input); } @Override protected void onStart() { super.onStart(); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.button_send_activity1: Intent Twointent = new Intent(this,MainActivity.class); startActivity(Twointent); break; case R.id.button_send_activity2: Intent Threeintent = new Intent(this,Main2Activity.class); startActivity(Threeintent); break; case R.id.button_takeout: variable = editText.getText().toString(); if (!"".equals(variable)) { //将输入的值赋给全局变量variable MyApplication.setGlobalvariable(variable); } textView.setText("将全局变量取出:"+MyApplication.getGlobalvariable()); break; default: } } }
到此本篇功能已经实现,欢迎讨论。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。