Android学习2——Activity之间跳转
程序员文章站
2022-05-14 13:57:07
...
原创内容,转载请注明出处
Activity是Android的四大控件之一,它是直接和用户打交道的界面,既然作为界面,那么界面和界面之间将会有一定的通讯,通讯则代表数据的传输。Activity之间的跳转通过Intent类完成,Intent类在跳转过程中还可携带数据。
修改activity_main.xml布局文件,添加一个文本框和发送按钮,点击发送按钮时,会将文本框的信息传到FirstActivity,并完成跳转。
<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" tools:context="com.cygoat.myapp.MainActivity" > <EditText android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/hello_world" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/send_button" android:onClick="sendMessage"/> </LinearLayout>
修改MainActivity.java,添加sendMessage(View)方法,当发送按钮点击时,会调用该方法。该方法会将文本框的消息保存到Intent对象中,并通过Intent实例跳转。代码如下
package com.cygoat.myapp;
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.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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);
}
public void sendMessage(View view){
Intent intent = new Intent(this,FirstActivity.class);
EditText text = (EditText) findViewById(R.id.message);
intent.putExtra("message", text.getText().toString());
startActivity(intent);
}
}
创建activity_first.xml布局,用来展示接收的消息,内容如下
<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.cygoat.myapp.FirstActivity" > <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
创建FirstActivity.java类,该类接收消息并展示。代码如下
package com.cygoat.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
Intent intent = getIntent();
String message = intent.getStringExtra("message");
TextView textView = (TextView) findViewById(R.id.result);
textView.setText("内容为:"+message);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, 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);
}
}
测试如下
源代码如附件