1.编写第1个Android应用程序实现按钮和复选框

来源:互联网 发布:进口软件 增值税 编辑:程序博客网 时间:2024/05/18 00:21

1>. 介绍Android驱动

<1. Android驱动 = Linux驱动 + 封装
<2. 封装指的是 : 服务,硬件访问服务

JNI方法访问硬件

2>. 第一个Android驱动程序 编写!

<1. 功能:通过几个按钮来控制开发板上的LCD灯


<2. 界面:一个大的总按钮,四个小的子按钮

APP界面


<3. View class:

<@. 直接之类 :AnalogClock、ImageView、KeyboardView、ProgressBar、SurfaceView、TextView、ViewGroup、ViewStub
<#. 间接之类 :Button等。


<4. 定义标签:
eg:

<控件名字
android:layout_width=”fill_parent” //排版宽度
android:layout_height=”wrap_content” //排版高度
android:text=”LED4” //控件显示的名字
android:onClick=”onCheckboxClicked” //当控件触发时启动的函数
android:id=”@+id/CheckBoxLed4” //控件的ID名, 在功能层调用findViewById(R.id.控件名);
/>


<5. 定义对象:
private Button buttonLead = null; //格式: 权限 类名 对象名 = 初始值;
方法一:
//实现对象方法
//采用匿名类
buttonLead.setOnClickListener(new View.OnClickListener() {

}
方法二:
//采用自己实现类
class MyButtonListener implements View.OnClickListener {

}
buttonLead.setOnClickListener(new MyButtonListener());


示例代码:
1<. MainActivity.java –APP层

package com.becauseican.app_0001_leddemo;import android.app.Activity;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;import java.util.concurrent.Future;public class MainActivity extends Activity {    private Button buttonLead = null;    private boolean boolLead = false;    private CheckBox checkBoxLed1 = null;    private CheckBox checkBoxLed2 = null;    private CheckBox checkBoxLed3 = null;    private CheckBox checkBoxLed4 = null;    /* 自己定义的类 */    class MyButtonListener implements View.OnClickListener {        @Override        public void onClick(View v) {            boolLead = !boolLead;            if (boolLead) {                buttonLead.setText("Close All");                checkBoxLed1.setChecked(true);                checkBoxLed2.setChecked(true);                checkBoxLed3.setChecked(true);                checkBoxLed4.setChecked(true);            }else {                buttonLead.setText("Open All");                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.CheckBoxLed4:                if (checked) {                    Toast.makeText(getApplicationContext(), "CheckBoxLed4 on", Toast.LENGTH_SHORT).show();                }                // Put some meat on the sandwich                else {                    Toast.makeText(getApplicationContext(), "CheckBoxLed4 off", Toast.LENGTH_SHORT).show();                }                // Remove the meat                break;            case R.id.CheckBoxLed3:                if (checked) {                    Toast.makeText(getApplicationContext(), "CheckBoxLed3 on", Toast.LENGTH_SHORT).show();                }                // Cheese me                else {                    Toast.makeText(getApplicationContext(), "CheckBoxLed3 off", Toast.LENGTH_SHORT).show();                }                // I'm lactose intolerant                break;            case R.id.CheckBoxLed2:                if (checked) {                    Toast.makeText(getApplicationContext(), "CheckBoxLed2 on", Toast.LENGTH_SHORT).show();                }                // Put some meat on the sandwich                else {                    Toast.makeText(getApplicationContext(), "CheckBoxLed2 off", Toast.LENGTH_SHORT).show();                }                // Remove the meat                break;            case R.id.CheckBoxLed1:                if (checked) {                    Toast.makeText(getApplicationContext(), "CheckBoxLed1 on", Toast.LENGTH_SHORT).show();                }                // Put some meat on the sandwich                else {                    Toast.makeText(getApplicationContext(), "CheckBoxLed1 off", Toast.LENGTH_SHORT).show();                }                // Remove the meat                break;            // TODO: Veggie sandwich        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        buttonLead = (Button) findViewById(R.id.ButtonLead);        checkBoxLed1 = (CheckBox) findViewById(R.id.CheckBoxLed1);        checkBoxLed2 = (CheckBox) findViewById(R.id.CheckBoxLed2);        checkBoxLed3 = (CheckBox) findViewById(R.id.CheckBoxLed3);        checkBoxLed4 = (CheckBox) findViewById(R.id.CheckBoxLed4);/*      // 采用匿名类的方式        buttonLead.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v){                boolLead = !boolLead;                if (boolLead) {                    buttonLead.setText("Close All");                }else {                    buttonLead.setText("Open All");                }            }        });*/        //使用自定义类的方式        buttonLead.setOnClickListener(new MyButtonListener());    }    @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);    }}

2<. activity_main.xml –样式文件

<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="Fogost·Tower"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"         />    <Button        android:text = "Open All"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/ButtonLead"        />    <CheckBox        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="LED1"        android:onClick="onCheckboxClicked"        android:id="@+id/CheckBoxLed1"        />    <CheckBox        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="LED2"        android:onClick="onCheckboxClicked"        android:id="@+id/CheckBoxLed2"        />    <CheckBox        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="LED3"        android:onClick="onCheckboxClicked"        android:id="@+id/CheckBoxLed3"        />    <CheckBox        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="LED4"        android:onClick="onCheckboxClicked"        android:id="@+id/CheckBoxLed4"        /></LinearLayout>
阅读全文
0 0
原创粉丝点击