Android游戏开发入门小结
——入门者参考
学习Android软件开发我首先感觉Android工程比我们以前写的java工程更加复杂,文件更多,但是当清楚每个文件时干嘛用的时候我发现Android开发不是那么的复杂,仅仅是文件多了而已,下面我以一个“helloword”工程为例,谈谈我对Android工程各个文件的理解。
src文件自然不用说了;gen目录下的R文件存放的是工程所用到的组件及图片等的id地址,便于我们调用;res目录下的drawable用来存放图片的;layout是布局文件;values文件是我们会用到的数据;当我们每new一个activity或者server的时候我们都需要在AndroidManifest.xml文件中添加说明。
在一个布局文件中我们可以添加各种组件,每个布局文件就相当于一个屏幕,在每个组件中我们可以设置其属性,定义其id,便于在java Class中调用。另外,每个我们也可以设置整个屏幕的属性,布局。
在Activity中我们可以得到组件,然后给各个组件进行处理,例如添加监听器等等。千万别忘了在AndroidManifest中添加新的activity哦。
接下来就是代码部分了
package first.hello;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class FirstActivity extends Activity {
//组件声明,定义属性
private Button bu_reset,bu_login;
private EditText et_userName,et_passWord;
//程序入口
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//显示界面
setContentView(R.layout.main);
//获取组件
getView();
bu_login.setOnClickListener(listener);
bu_reset.setOnClickListener(listener);
}
/**
* 获取屏幕上的组建对象的方法
*/
public void getView(){
et_userName=(EditText)findViewById(R.id.et_userName);
et_passWord=(EditText)findViewById(R.id.et_passWord);
bu_login=(Button)findViewById(R.id.bu_login);
bu_reset=(Button)findViewById(R.id.bu_reset);
}
//创建监听器
OnClickListener listener=new OnClickListener(){
public void onClick(View v){
//获取被点击按钮的id
int id=v.getId();
switch(id){
case R.id.bu_login:
System.out.println("userName=="+et_userName.getText()+" passWord=="+et_passWord.getText());
if("aaa".equals(et_userName.getText().toString())&&"aaa".equals(et_passWord.getText().toString())){
//提示信息
//Toast.makeText(FirstActivity.this, "登陆成功", 3).show();
//创建intent对象,相当于信使rna
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
//传参数
intent.putExtra("userName", et_userName.getText().toString());
//跳转
startActivity(intent);
}else{
//提示信息
Toast.makeText(FirstActivity.this,"登录失败",3);
}
break;
case R.id.bu_reset:
//清空
et_userName.setText("");
et_passWord.setText("");
break;
}
}
};
}
package first.hello;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//显示界面
setContentView(R.layout.second);
//获取Intent对象
Intent intent = getIntent();
//获取参数
String username = intent.getStringExtra("userName");
//得到文本组件,并设置其文本
TextView tv_userName = (TextView)findViewById(R.id.tv_userName);
tv_userName.setText(username);
}
}
<?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/userName"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/et_userName"
android:textSize="30dip"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/passWord"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/et_passWord"
android:textSize="30dip"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reset"
android:id="@+id/bu_reset"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:id="@+id/bu_login"
/>
</LinearLayout>
<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_userName"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="first.hello"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="FirstActivity"
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=".SecondActivity"
/>
</application>
</manifest>