android开发中需要在不同activity间传值,这里介绍使用intent在两个activity间传值,为了清晰的看到效果,这里以发送消息的形式进行。
//用来发送消息的activity的布局文件
</LinearLayout>
<LinearLayout 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:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.radiobarseekbar.indexactivitybuttons.MainActivity">
<TextView
android:text="主界面:用来发送值"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:id="@+id/tvSend"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:id="@+id/etSend"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="点击,发送消息到SecondActivity"
android:id="@+id/btnSend"/>
</LinearLayout>
下面是用来发送消息的activity的.java文件
package com.example.radiobarseekbar.indexactivitybuttons;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tvSend;
private EditText etSend;
private Button btnSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvSend = (TextView) findViewById(R.id.tvSend);
etSend = (EditText) findViewById(R.id.etSend);
btnSend = (Button) findViewById(R.id.btnSend);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String strSendMsg = etSend.getText().toString();
Intent intent = new Intent();
intent.putExtra("msg",strSendMsg);
intent.setClass(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
//下面是用来接收传值的activity的.java文件、
package com.example.radiobarseekbar.indexactivitybuttons;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
/**
* Created by fyy on 15-4-2.
*/
public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = this.getIntent();
String strGetMsg = intent.getStringExtra("msg");
Toast.makeText(SecondActivity.this,"接收到的消息是:"+strGetMsg,Toast.LENGTH_SHORT).show();
}
}