Android 《第一行代码》 第二章练习代码 ActivityTest

FirstActivity.java

 package com.example.activitytest;

 import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast; public class FirstActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); /*
* 隐藏标题栏,定要在setContentView()之前执行 ,否则会报错 java.lang.RuntimeException:
* Unable to start activity ComponentInfo:
* android.util.AndroidRuntimeException: requestFeature() must be called
* before adding content
*/
requestWindowFeature(Window.FEATURE_NO_TITLE); // 加载布局
setContentView(R.layout.first_layout); Button button1 = (Button) findViewById(R.id.button_1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// toast提示
// Toast.makeText(FirstActivity.this, "You click Button 1",
// Toast.LENGTH_SHORT).show(); // 销毁活动
// finish(); // Intent实现活动跳转,显示Intent
// Intent intent = new Intent(FirstActivity.this,
// SecondActivity.class);
// startActivity(intent); // 隐式Intent
// Intent intent = new
// Intent("com.example.activitytest.ACTION_START");
// intent.addCategory("com.example.activitytest.MY_CATEGORY");
// startActivity(intent); // 启动其他程序的活动
// Intent intent = new Intent(Intent.ACTION_DIAL);
// // intent.setData(Uri.parse("http://www.baidu.com"));
// intent.setData(Uri.parse("tel:10086"));
// startActivity(intent);
//
// 用intent传递数据
// String data = "Hello SecondActiyity";
// Intent intent = new Intent(FirstActivity.this,
// SecondActivity.class);
// intent.putExtra("extra_data", data); //键,值
// startActivity(intent); // 返回数据给上一个活动
Intent intent = new Intent(FirstActivity.this,
SecondActivity.class);
startActivityForResult(intent, 1); } });
} /* 由于我们是使用startActivityForResult()方法来启动SecondActivity 的,在SecondActivity
被销毁之后会回调上一个活动的onActivityResult()方法
第一个参数requestCode,即我们在启动活动时传入的请求码。第二个参数resultCode,即我们在返回数据时传入的处理结果。
第三个参数data,即携带着返回数据的Intent。*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
String returnedData = data.getStringExtra("data_return");
Log.d("FirstActivity", returnedData);
}
break;
default:
}
} // 显示菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} // 菜单相应事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_item:
Toast.makeText(this, "You clicked add", Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this, "You clicked remove", Toast.LENGTH_SHORT)
.show();
break;
default:
}
return true;
}
}

SecondActivity.java

 package com.example.activitytest;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button; public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second_layout);
// 用intent传递数据
// Intent intent = getIntent();
// String data = intent.getStringExtra("extra_data");
// Log.d("SecondActivity",data); // 返回数据给上一个活动
Button button2 = (Button) findViewById(R.id.button_2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("data_return", "Hello FirstActivity");
setResult(RESULT_OK, intent);
finish();
}
});
} // 通过按下Back 键回到FirstActivity
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("data_return", "Hello FirstActivity");
setResult(RESULT_OK, intent);
finish();
} }

first_layout.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"
android:orientation="vertical" > <!--@+id/id_name:在XML中定义一个id,
@id/id_name:引用一个id
wrap_content:示当前元素的高度只要能刚好包含里面的内容就行 -->
<Button
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"
/> </LinearLayout>

/ActivityTest/res/menu/main.xml

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/add_item"
android:title="Add" />
<item
android:id="@+id/remove_item"
android:title="Remove" /> </menu>

/ActivityTest/AndroidManifest.xml

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitytest"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- .FirstActivity :com.example.activitytest.FirstActivity 的缩写 -->
<activity
android:name=".FirstActivity"
android:label="This is FirstActivity">
<intent-filter>
<!-- 点击桌面应用程序图标时首先打开的就是这个活动 -->
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity> <activity android:name=".SecondActivity">
<intent-filter>
<!--<action>和<category>中的内容同时能够匹配上Intent中指定的action 和category时,
这个活动才能响应该Intent。-->
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.activitytest.MY_CATEGORY"/>
</intent-filter>
</activity>
<!--可以相应打开网页的intent-->
<activity android:name=".ThirdActivity" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
</activity>
</application> </manifest>
上一篇:在文件夹快速启动cmd或powershell


下一篇:win10 在当前目录下 打开cmd