分享

Android学习笔记(1)

 quasiceo 2015-06-30

Android学习笔记(1)

activity的重要信息

1.一个activity是一个类,需要继承activity

2.需要重写onCreate方法

3.每个activity都需要在AndroidManigest.xml文件中注册,有filter的是第一个运行的。

4.为activity添加必要控件。

(事半功倍的android activity绘制工具,DroidDraw http://code.google.com/p/droiddraw/

intent的重要信息:

component name  应用程序的名字

action        动作(发短信)

data        传送数据uri(发短信)

category           暂未用到

extras       键值对

flags         暂未用到

下面将我写的一个程序的源代码放上来:

复制代码
package com.lee.android;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
private Button button1;
private Button button2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1
= (Button)findViewById(R.id.button1);
button1.setText(
"press me to textView2");
TextView textView1
= (TextView)findViewById(R.id.textview1);
textView1.setText(
"i'm textView1");
// button1.setOnClickListener(new MyButtonListener());

// 匿名类写法,监听模式
button1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
Intent intent
= new Intent();
intent.putExtra(
"name", "wahaha");
intent.setClass(HelloAndroid.
this,Activity2.class );
startActivity(intent);
HelloAndroid.
this.finish();
}
}
);

button2
= (Button)findViewById(R.id.button2);
button2.setText(
"按我发送短信");
button2.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
Uri uri
= Uri.parse("smsto:13200000000");
Intent sendM
= new Intent(Intent.ACTION_SENDTO,uri);
//所谓intent的action
//uri为数据data
sendM.putExtra("sms_body:", "Message content");
//extras一些键值对信息
startActivity(sendM);
}
});




}
// 内部类写法
// class MyButtonListener implements Button.OnClickListener{
//
// @Override
// public void onClick(View v) {
// Intent intent = new Intent();
// intent.putExtra("name", "wahaha");
// //给这个intent设置一个键值对,传递给下一个activity
// intent.setClass(HelloAndroid.this, Activity2.class);
// startActivity(intent);
// HelloAndroid.this.finish();
//
// }
// }


}
复制代码

 Activity2.class

复制代码
package com.lee.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Activity2 extends Activity {
private Button button1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
// 调用布局main
Intent intent = getIntent();
// 得到传递过来的Intent,并且设置为text的value
String value = intent.getStringExtra("name");
button1
= (Button) findViewById(R.id.button2);
// 拿到button
button1.setText("press me to textView1");
TextView textView1
= (TextView) findViewById(R.id.textview2);
textView1.setText(value);

button1.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
Intent intent
= new Intent();
intent.setClass(Activity2.
this, HelloAndroid.class);
startActivity(intent);
Activity2.
this.finish();
}
});
// 内部类和匿名类的写法,这里是匿名类的写法
}
}
复制代码

main.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height
="fill_parent"
xmlns:android
="http://schemas./apk/res/android"
android:orientation
="vertical"
>
<TextView
android:id="@+id/textview1"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
>
</TextView>
<Button
android:id="@+id/button1"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
>
</Button>

<Button
android:id="@+id/button2"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content">
</Button>
</LinearLayout>
复制代码

  这个简单的小程序,主要演示了如何使用intent,以及在同一个应用程序之间的intent切换,和不同应用程序之间的切换(发短信),其中简单解释了intent。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多