初次接触android开发,知道android是由一个一个活动组成的。。。写了一个主界面控制副界面的代码。
android里的目录:src存java代码;gen存android资源文件标识符,不需要自己维护;assets保存应用的资源文件,如音频或视频等不经常被用户修改的文件;bin包含编译生成的apk的应用程序xx.apk;lib包含第三方类库jar包;res中drawable高清超高清文件,layout布局文件,完成ui控件的堆放,menu菜单,values字符资源文件。
每一个activity都要套一个xml的layout文件。具体代码如下
MainActivity.java
package com.example.tree;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Button button;//xml文件下的button
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)this.findViewById(R.id.button1);//此处用R文件中的id固定的button
button.setOnClickListener(new View.OnClickListener() {//为button设置一个监听器
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//以下皆为从这个activity切换入下一个activity的方法,后两种需要在AndroidManifest文件中设置内容 具体内容见Andr //oidManifest文件
// 第一种
Intent intent=new Intent(MainActivity.this,NextActivity.class);
intent.putExtra("name", "jack");//利用intent传值给下一个activity
intent.putExtra("age", 23);
intent.putExtra("address", "beijing");
Bundle bundle=new Bundle();//用bundle传一组值
bundle.putString("code", "1001");
intent.putExtra("bundle", bundle);
startActivity(intent);
//第二种
// Intent intent=new Intent();
// intent.setClass(MainActivity.this, NextActivity.class);
// startActivity(intent);
//第三种
// Intent intent=new Intent("com.example.tree.NextActivity");
// startActivity(intent);
//第四种
// Intent intent=new Intent();
// intent.setAction("com.example.tree.NextActivity");
// 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);
}
}
NextActivity.java
package com.example.tree;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class NextActivity extends Activity {
private final String TAG="NextActivity";//输出值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
Intent intent=getIntent();
/* String name=intent.getStringExtra("name");
System.out.println("--name->>"+name);
int age=intent.getIntExtra("age", 0);
System.out.println("--age->>"+age);
String address=intent.getStringExtra("address");
System.out.println("--address->>"+address);
Bundle bundle=intent.getBundleExtra("bundle");
System.out.println("--code->>"+bundle.getString("code"));*/
String name=intent.getStringExtra("name");
Log.i(TAG, "-name->>"+name);
int age=intent.getIntExtra("age", 0);
Log.i(TAG, "-age->>"+age);
String address=intent.getStringExtra("address");
Log.i(TAG, "-address->>"+address);
Bundle bundle=intent.getBundleExtra("bundle");//一开始bundle写成了Bundle。。。。错了好久没找到。。
String code=bundle.getString("code");
Log.i(TAG, "-code->>"+code);
}
}
activity_main.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.example.tree.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="45dp"
android:layout_marginTop="50dp"
android:text="enter next" />
</RelativeLayout>
activity_next.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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="48dp" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tree"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NextActivity"
android:label="@string/title_activity_next" >
<intent-filter>
<action android:name="com.example.tree.NextActivity"/>//MainActivity.java中方法3、4中需要的部分
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>