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+"");
}
}
}
第一个程序效果图
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开发书籍推荐:从入门到精通系列学习路线书籍介绍
- [Kotlin&Anko开发Android入门学习笔记]-02Kotlin如何使用Android第三方库
- android学习之快速入门1--android工程文件分析
- 【Android学习入门】Android中activity的启动模式
- 分享Android入门学习资料
- Android Support Library 学习入门
- Android学习整理(一)入门基础--1、eclipse工程结构
- android入门基础总结(2) -----学习小结
- Android入门学习笔记整理(三)
- Android入门学习笔记(三)|Activity和Intent
- Android Studio 学习笔记-新手入门-(2)导入项目遇到的问题
- Android入门学习笔记整理(五)
- android之RxJava的学习,从浅到深,从入门到别放弃(二)
- Android入门学习(二)——布局管理器
- Android开发书籍推荐:从入门到精通系列学习路线书籍介绍
- Android中关于JNI 的学习(零)简单的样例,简单地入门
- android 入门学习笔记 Button 常用的弹窗效果code
- android开发学习(四)——anr产生的原理&如何避免,android消息机制入门, 网络图片查看器
- Android开发入门之学习笔记(二)