activity的使用(八):生命周期

本文介绍了一个简单的Android应用程序示例,该程序通过监听特定按键触发事件来启动新的Activity页面,并在两个页面间传递数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主activity,当按下'A'键时,进入第二个activity

package com.example.androidtest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.KeyEvent;
import android.widget.Toast;
import android.util.Log;

public class AndroidTest extends Activity {
	  
	String tag = "Events";
	int request_code = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_android_test);
		
		Log.d(tag, "In the onCreate() event");
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		Log.d(tag, "code: " + keyCode + " keycode:" + KeyEvent.KEYCODE_BUTTON_6);
		if (keyCode == 29) {
			Intent i = new Intent(AndroidTest.this, Activity2.class);
			Bundle extras = new Bundle();
			extras.putString("Name", "Your name here");
			i.putExtras(extras);
			startActivityForResult(i, 1);
		}
		
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.android_test, menu);
		return true;
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (requestCode == 1) {
			if (resultCode == RESULT_OK) {
				Toast.makeText(this, data.getData().toString(), Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	public void onStart()
	{
		super.onStart();
		Log.d(tag, "In the onStart() event");
	}
	
	public void onResume()
	{
		super.onResume();
		Log.d(tag, "In the onResume() event");
	}
	
	public void onRestart()
	{
		super.onRestart();
		Log.d(tag, "In the onRestart() event");
	}
	
	public void onPause()
	{
		super.onPause();
		Log.d(tag, "In the onPause() event");
	}
	
	public void onStop()
	{
		super.onStop();
		Log.d(tag, "In the onStop() event");
	}
	
	public void onDestroy()
	{
		super.onDestroy();
		Log.d(tag, "In the onDestroy() event");
	}
}

其xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world"
    />
</LinearLayout>


第二个activity:

package com.example.androidtest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.content.Intent;
import android.net.Uri;

public class Activity2 extends Activity {
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.row);
		
		String defaultName = "";
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			defaultName = extras.getString("Name");
		}
		
		EditText txt_username = (EditText)findViewById(R.id.txt_username);
		txt_username.setHint(defaultName);
		
		Button btn = (Button)findViewById(R.id.btn_OK);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent data = new Intent();
				EditText txt_username = (EditText)findViewById(R.id.txt_username);
				data.setData(Uri.parse(txt_username.getText().toString()));
				setResult(RESULT_OK, data);
				finish();
			}
		});
	}

}

xml为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Please enter your name" />    
<EditText
    android:id="@+id/txt_username"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" />    
<Button
    android:id="@+id/btn_OK"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="OK" />
</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值