Android开发之编写第一个Android应用程序实现按钮和复选框

简介: 搞Android系统这么久了,说实话,我连apk怎么写还真是不会,说实话能够看懂,简单改改就不错了,说来惭愧,我是嵌入式出身。最近开始学习Android应用开发,和我的底层结合起来,为了工作,咬着牙也要学下去!!! 首先,我使用的是Android Studio这个软件。

搞Android系统这么久了,说实话,我连apk怎么写还真是不会,说实话能够看懂,简单改改就不错了,说来惭愧,我是嵌入式出身。最近开始学习Android应用开发,和我的底层结合起来,为了工作,咬着牙也要学下去!!!奋斗

首先,我使用的是Android Studio这个软件。

上谷歌中国网就可以下载到了,地址如下:

https://developer.android.google.cn/develop/index.html

    编译环境配置,找百度看看就行了,这里不哆嗦。

    安装好后,有一些工具没有安装到位可能会报下面类似的错误,看着下面的log提示找解决方案就行了,不懂就百度。作为一个开发人员,错误信息要会看。


程序实现的界面,功能如下:

(1)按下ALL ON/OFF,所有的复选框checkbox被全部选中或者全部不选中,且按钮里的字符串变成ALL ON或者ALL OFF,并打印消息led all on或者led all off

(2)勾选或者不勾选checkbox,会选中对应的LEDx,并打印对应LED on或者 LED off

     

首先看下Activity_main.xml,这个文件是以上这个界面的布局文件,路径在res/layout/activity_main.xml

笔记:

(1)Layout的宽度和高度,用wrap_content表示取决于它的内容

android:layout_width="wrap_content"
android:layout_height="wrap_content"

(2)表示宽度会填充整个窗口

fill_parent

(3)线性垂直

把RelativeLayout 改为如下xml代码的 LineraLayout

android:orientation="vertical" 这一行表示垂直
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:orientation="vertical"
    >
    @顺序排列--->按垂直方向
    <TextView android:text="First Android progame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    @指定ID
    <Button
        android:id="@+id/BUTTON"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ALL ON/OFF"
        />
    <CheckBox
        android:id="@+id/LED1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="LED1"
        android:onClick="onCheckboxClicked"
        />
    <CheckBox
        android:id="@+id/LED2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="LED2"
        android:onClick="onCheckboxClicked"
        />
    <CheckBox
        android:id="@+id/LED3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="LED3"
        android:onClick="onCheckboxClicked"
        />
    <CheckBox
        android:id="@+id/LED4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="LED4"
        android:onClick="onCheckboxClicked"
        />

</LinearLayout>
如何在布局中添加控件?如上,要添加一个控件,那么就 <控件名称  />  在这里面添加相关的信息。

最关键的就是id。Android Studio这个软件很智能,有自动补全功能,没有的上网百度或者参考文档。

如图,打开谷歌中国,右上角,搜索,比如Button:


搜索Button会弹出:


选择android.widget.Button,会弹出以下界面:

参考的编写代码,案例都在下面,看看就会写了,非常简单。


接下来是写java代码,同样的,不管是按钮也好,复选框也好,不知道怎么写就上这个谷歌中国,搜索,就可以找到代码了。

在java中有MainActivity中有MainActivity.java中添加代码:

package com.example.administrator.app_0001_leddemo;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
//根据文档导入类
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    //定义一个按钮对象
    private boolean ledon = false ;
    private  Button button = null ;
    private CheckBox checkBoxLed1 = null ;
    private CheckBox checkBoxLed2 = null ;
    private CheckBox checkBoxLed3 = null ;
    private CheckBox checkBoxLed4 = null ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //在这里添加代码
        button = (Button)findViewById(R.id.BUTTON);
        //找到对应的ID
        checkBoxLed1 = (CheckBox)findViewById(R.id.LED1);
        checkBoxLed2 = (CheckBox)findViewById(R.id.LED2);
        checkBoxLed3 = (CheckBox)findViewById(R.id.LED3);
        checkBoxLed4 = (CheckBox)findViewById(R.id.LED4);

        //拷贝文档中的参考代码,设置监听器 ---> 匿名类写法
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Code here executes on main thread after user presses button
                ledon = !ledon ;
                if(ledon) {
                    button.setText("ALL_ON");
                    Toast.makeText(getApplicationContext(),"led all on",Toast.LENGTH_SHORT).show();
                    //表示全部选中
                    checkBoxLed1.setChecked(true);
                    checkBoxLed2.setChecked(true);
                    checkBoxLed3.setChecked(true);
                    checkBoxLed4.setChecked(true);
                }
                else {
                    button.setText("ALL_OFF");
                    Toast.makeText(getApplicationContext(),"led all off",Toast.LENGTH_SHORT).show();
                    //表示全部不选中
                    checkBoxLed1.setChecked(false);
                    checkBoxLed2.setChecked(false);
                    checkBoxLed3.setChecked(false);
                    checkBoxLed4.setChecked(false);
                }
            }
        });
    }

    public void onCheckboxClicked(View view) {
        // Is the view now checked?
        boolean checked = ((CheckBox) view).isChecked();

        // Check which checkbox was clicked
        switch(view.getId()) {
            case R.id.LED1:
                //表示选中
                if (checked) {
                    //打印
                    Toast.makeText(getApplicationContext(),"led1 on",Toast.LENGTH_SHORT).show();
                }
                //表示不选中
                else {
                    Toast.makeText(getApplicationContext(),"led1 off",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.LED2:
                if (checked) {
                    Toast.makeText(getApplicationContext(),"led2 on",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(getApplicationContext(),"led2 off",Toast.LENGTH_SHORT).show();
                }
                break;
            // TODO: Veggie sandwich
            case R.id.LED3:
                if (checked) {
                    Toast.makeText(getApplicationContext(),"led3 on",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(getApplicationContext(),"led3 off",Toast.LENGTH_SHORT).show();
                }
                break ;
            case R.id.LED4:
                if (checked) {
                    Toast.makeText(getApplicationContext(),"led4 on",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(getApplicationContext(),"led4 off",Toast.LENGTH_SHORT).show();
                }
                break ;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
编写好了,点击运行,会让你选择:


没有Android设备就默认OK就行了:


最终看到效果,大功告成:


目录
相关文章
|
20天前
|
存储 消息中间件 人工智能
【03】AI辅助编程完整的安卓二次商业实战-本地构建运行并且调试-二次开发改注册登陆按钮颜色以及整体资源结构熟悉-优雅草伊凡
【03】AI辅助编程完整的安卓二次商业实战-本地构建运行并且调试-二次开发改注册登陆按钮颜色以及整体资源结构熟悉-优雅草伊凡
56 3
|
16天前
|
开发工具 Android开发
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
214 11
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
|
20天前
|
存储 消息中间件 人工智能
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
65 11
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
|
26天前
|
Java 开发工具 Maven
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
97 6
|
3月前
|
安全 数据库 Android开发
在Android开发中实现两个Intent跳转及数据交换的方法
总结上述内容,在Android开发中,Intent不仅是活动跳转的桥梁,也是两个活动之间进行数据交换的媒介。运用Intent传递数据时需注意数据类型、传输大小限制以及安全性问题的处理,以确保应用的健壯性和安全性。
186 11
|
3月前
|
移动开发 Java 编译器
Kotlin与Jetpack Compose:Android开发生态的演进与架构思考
本文从资深Android工程师视角深入分析Kotlin与Jetpack Compose在Android系统中的技术定位。Kotlin通过空安全、协程等特性解决了Java在移动开发中的痛点,成为Android官方首选语言。Jetpack Compose则引入声明式UI范式,通过重组机制实现高效UI更新。两者结合不仅提升开发效率,更为跨平台战略和现代架构模式提供技术基础,代表了Android开发生态的根本性演进。
116 0
|
Android开发
Android--退出整个应用程序
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/52229304 在写android应用程序时,经常会遇到想退出当前Acitivity,或者直接退出应用程序.我之前的一般操作是按返回键,或者直接按home键直接返回,其实这两种操作都没有关闭当前应用程序,没有释放系统资源。
945 0
|
安全 Android开发 Java
我的Android进阶之旅------&gt;Android安全退出应用程序的几种方式
当应用不再使用时,通常需要关闭应用,可以使用以下几种方法关闭android应用: 第一种方法:首先获取当前进程的id,然后杀死该进程。  建议使用这种方式 android.
1335 0

热门文章

最新文章