自学Android开发的入门过程

最近一周以来,在研究Android开发的入门,因为本身做java的,所以相对来说应该会比较容易。

习惯用的IDE是eclipse,所以自然就通过它来集成Android开发的环境。各种百度后确认了集成思路:下载sdk的自己的电脑,在IDE中安装ADT插件并指定sdk,通过ADV下载不同版本的tools,新建Android项目,编写代码并运行。

一切看起来是那么的自然,通畅。可但是,迎面扑来的事实像润物细无声的春雨洒向大地后长出来的竹笋,各种问题出现的也是那么自然,那么酣畅淋漓。

sdk下载的慢,adt下载的慢,tools下载的慢,sdk和adt之间的版本问题,tools不同版本对项目的影响,新建项目时选择的版本导致R类的报错……这还算是我能记住的,在实际中遇到,解决的小点已经记不得了。好在我都一一解决了,项目可以正常跑起来。但可是最让我不能容忍的是:layout.xml文件在eclipse中居然像青铜般倔强的不显示可视化控件,只能代码编辑。从功能上说并不影响什么,可是我骗不了自己的心,不完美的东西总会让人遗憾。已经到了这步,轻言放弃纯属扯淡。根据提示是说ADT版本低于sdk版本导致的。于是乎升级ADT,下载低版本tools别标记为首要任务,下载慢也认了。一晚后怀揣着理想的心再次打开layout文件,有种想骂人的冲动。不是说好的嘛,我ADT到24了,sdk降到23,为什么还是提示adt过低。

于是乎,果断转投了Android studio。不是不能轻易放弃吗,那个……呃,我是有理由的(说的再好听也TM没做成)

幸好之前用过idea,Android studio用起来还算顺手。这个专门开发Android的软件集成度很高,指定个sdk就行。直接建项目开始操作,反正到时候需要什么它会提示下载。于是乎,按照之前想好的,新建了一个 计算器 的项目后,发现我靠,这样一个项目是不是过于简单,我可不是说只能运行个项目就算,我是要学开发的。于是乎就有了下面的项目目录:


刚新建完成时,只有 MainActivity类 和 activity_main.xml文件(其他东西暂时忽略,不懂,也用不到。)按照我的理解,这两个加在一起就相当于网页中的 js 和 html标签。

相同的是:在js 和 MainActivity 中定义事件,在html 和 activity_main.xml 中建立布局

不同的是:MainActivity是一个class,在其中指定是对应的哪个xml布局文件。

至于带first,second的是我自建的,带content的我都不知道怎么出现的。

一个app的首页就是 activity_main.xml 和 MainActivity 中定义的内容,相当于index.html。模拟机的页面如下:


简单,很简单,它什么都没有,就一个能跳转的控件。对应的 layout_main.xml 的代码是:

<?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"
    android:orientation="horizontal">
    
<!--按钮控件,定义了其中的属性-->
    <Button
        android:id="@+id/next_to"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="跳转"
        android:textAlignment="center"
        android:textColor="@android:color/holo_red_light"
        android:textSize="30sp"
        android:textStyle="bold" />

</LinearLayout>
MainActivity类中的代码:
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;

public class MainActivity extends Activity{

    private Button next = null;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//该方法指定activity与layout文件的对应关系

        next = (Button) findViewById(R.id.next_to);//通过id查找已经定义好的控件
        next.setOnClickListener(new OnClickListener() {//绑定监听事件,内容为点击该控件跳转到另一个activity,不传参
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,FirstActivity.class);
                startActivity(intent);
            }
        });
    }

}
效果是点击 “跳转” 按钮,会调到另一个Activity中:包含 first 的,纯跳转,没有参数,跳到的页面:

这个里面包含了对于基本布局的应用,和 6 个控件(水平和垂直布局中各有2个),对应的 activity_first.xml 内容如下:

<?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"
    android:orientation="vertical">

<!--第一个:水平-->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ui_basic_name"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ui_name"/>

    </LinearLayout>
<!--第二个:垂直-->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ui_basic_address"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ui_address"/>
    </LinearLayout>

    <Button
        android:id="@+id/just1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳转到另一个Activity" />

    <Button
        android:id="@+id/nextapp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动另一个app" />

</LinearLayout>
其中的 “姓名:”、“地址:” 不是写死的,而是在values目录中的 strings.xml 文件中定义好的,如下:

<resources>
    
    <string name="app_name">Calputer</string>
    <string name="action_settings">Settings</string>
    
    <string name="ui_basic_name">姓名:</string>
    <string name="ui_basic_address">地址:</string>

</resources>
而 button 中的内容就是写死的,使用 strings.xml 中预定义的也行。而 “白痴”、“上海” 则来自于FirstActivity中动态添加进去的,如下:

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

public class FirstActivity extends Activity {

    private Button activityButton = null;
    private Button app = null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

//获取已经在layout中定义好的控件,并添加数据
        TextView nameValue = (TextView) findViewById(R.id.ui_name);
        nameValue.setText("白痴");
        TextView addressValue = (TextView) findViewById(R.id.ui_address);
        addressValue.setText("上海");

//获取该button,设置监听事件内容为:跳转到另一个activity
        activityButton = (Button) findViewById(R.id.just1);
        activityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(FirstActivity.this,SecondActivity.class);

                //用Bundle携带数据
                Bundle bundle=new Bundle();
                bundle.putString("test", "熊是怎么死的?");
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

//获取该button,设置监听事件内容为:启动另一个自定义的app,同时传了一个名为“edit”的参数
        app = (Button) findViewById(R.id.nextapp);
        app.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);

                ComponentName cn = new ComponentName("cn.lung.otherapp","cn.lung.otherapp.MainActivity");

                intent.putExtra("edit","这就是我往另一个app传的内容");
                intent.setComponent(cn);
                startActivity(intent);
            }
        });

    }}
其中不止填充了数据,还对两个 button 分别设置了点击事件,用来:跳转activity时传参 和 启动另外一个app

第一个button和之前主页中的类似,只是多了一个要传递的参数:“test”,跳转过去的 activity : second 如下:



可以看到该 activity 拿到了参数,至于它上面的 button 就是在 activity_second.xml 中之前定义的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.mycompany.calputer.SecondActivity">

    <Button
        android:id="@+id/app1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动系统自带的程序:搜索" />
    

    <TextView
        android:id="@+id/test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:textColor="@android:color/holo_orange_light"
        android:textSize="24sp"
        android:textStyle="bold" />


</LinearLayout>
这个只是定义布局,参数则是在 SecondActivity 中接收的:

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

public class SecondActivity extends Activity {

    private Button button = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

       button = (Button) findViewById(R.id.app1);
       button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
               intent.putExtra(SearchManager.QUERY,"想搜啥,赶紧的");
               startActivity(intent);
           }
       });

        Bundle bundle = getIntent().getExtras();//接收参数,并填充进指定控件
        String test = bundle.getString("test");
        TextView view = (TextView) findViewById(R.id.test);
        view.setText(test);
    }

}
至于其中的 button 控件,我添加的是事件是:点击后调用系统自带的浏览器程序。后来我又加了一个调用拨号盘的功能,就是在 layout_second.xml 中加入下面代码:
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动系统自带程序:电话" />
    <!--通过autoLink属性指定该链接为调用拨号盘功能,并传参数-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="phone"
        android:text="1856481" />
显示页面就是这样了:(我把新控件夹在了原先两个控件中间)



当点击搜索控件时,就会调用了:

当点击电话下面的数字,会调用拨号盘:



至于启动另一个app,在前面已经给出代码。先看一下直接打开另一个App的 activity :


就是定义了两个控件 : TextView(数据填充:“原住民”) 和 EditView (在该项目的 MainActivity 中,接收前面一个app传来的数据动态填充)

被调用后打开的 activity 页面:


至此,app内不同 activity 的相互跳转,参数传递,调用系统程序和其他app 的功能完毕,我知道还有其他的方法完成这样的功能,但是目前还不会。

至于数据和服务端的交互,过段时间再研究一下


总结:

1.可以在模拟机中的 tool-option选项中更改链接来改善下载的速度

2.控件可以在layout布局文件中静态建立,也可以在activity类中动态创建

3.如果给控件设立监听事件,并且想要传递参数进去的话。可以通过自己实现onClickListener接口中的方法,自定义一个监听

4.参数的传递可以通过 Bundle,也可以通过 Intent


LG





  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LUNG108

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值