您的位置:首页 > 移动开发 > Android开发

Android入门学习

2012-08-30 23:58 302 查看
准备工作 SDK,ADT,Eclipse
第一个程序效果图



1.先说下布局文件,路径:res/layout/xx.xml
<TableLayout 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" >
<EditText
android:id="@+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
/>
<TextView
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<EditText
android:id="@+id/ed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableLayout>
2.第一个Activity
路径: src/包名/xx.java
package com.example.activity05;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
//Activity需要继承Activity类,并复写onCreate方法

public class Activity_05 extends Activity {
private EditText ed1;
private TextView tv;
private EditText ed2;
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_05);
//通过findViewById()找到各个控件
ed1=(EditText)findViewById(R.id.ed1);
tv=(TextView)findViewById(R.id.view);
ed2=(EditText)findViewById(R.id.ed2);
btn=(Button)findViewById(R.id.btn);
/*可设计为btn.setText(R.string.变量); ,变量需定义在string.xml文件中 ,可方便软件国际化*/
btn.setText("click me");
//给按钮设计监听器
btn.setOnClickListener(new btnListener());

}
//监听器类,需要实现OnClickListener类
class btnListener implements OnClickListener{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
创建Intent对象
Intent intent = new Intent();
//setClass方法用于从当前activity跳到另一个activity
intent.setClass(Activity_05.this,resultActivity.class);
String e1=ed1.getText().toString();
String e2=ed2.getText().toString();
//使用intent对象给另一个Activity传递数据
intent.putExtra("ed1",e1);
intent.putExtra("ed2",e2);
Activity_05.this.startActivity(intent);
}
}
}
3.所有的activity需要在AndroidManifest.xml中声明才可以使用
所以resultActivity需要在AndroidMainfest.xml中声明才可以使用。
声明代码
<activity
android:name=".resultActivity">
</activity>
4.第二个activity代码如下
package com.example.activity05;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class resultActivity extends Activity {

private TextView t;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//需要在layout中添加这个activity的布局文件result_activity.xml
setContentView(R.layout.result_activity);
Intent intent=getIntent();
String a =intent.getStringExtra("ed1");
String b =intent.getStringExtra("ed2");
if(a==null||b==null)
{
t=(TextView)findViewById(R.id.view1);
t.setText("数据为空");
}
else
{
int c=Integer.parseInt(a);
int d=Integer.parseInt(b);
t=(TextView)findViewById(R.id.view1);
t.setText(c*d+"");

}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  程序 学习 Android