Application值传递。

1、layout下面的布局

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.app.androidvaluespass.MainActivity"> <Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="值传递"/> </RelativeLayout>

values.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"> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Msg"/> </LinearLayout>

2、类及启动项

MainActivity

package com.example.app.androidvaluespass;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity { private Button btn;
private MyApp myApp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)this.findViewById(R.id.btnSubmit);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myApp = (MyApp)getApplication();
myApp.setName("赵信!");//修改后的名称
Intent intent = new Intent(MainActivity.this,ValuesActivity.class);
startActivity(intent);
}
});
}
}

定义一个Application类 MyApp

package com.example.app.androidvaluespass;

import android.app.Application;

/**
* Created by honghong on 2016/6/18.
*/
public class MyApp extends Application { public String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public void onCreate() {
super.onCreate();
setName("张三");
}
}

定义一个接收值的类

package com.example.app.androidvaluespass;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView; /**
* Created by honghong on 2016/6/18.
*/
public class ValuesActivity extends Activity { private MyApp myApp;
private TextView editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.values);
editText = (TextView)this.findViewById(R.id.Msg);
myApp = (MyApp)getApplication();
editText.setText(myApp.getName());
}
}

最后要在AndroidManifest.xml 文件中配置一下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app.androidvaluespass"> <application
android:name=".MyApp" // ← 继承Application类的名称,这个一定要对应。
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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=".ValuesActivity"></activity> // 这个类调用Application类ValuesActivity
</application> </manifest>
上一篇:Java 操纵XML之创建XML文件


下一篇:【转】Linux Posix Timer使用