1.编写第1个Android应用程序实现按钮和复选框
来源:互联网 发布:进口软件 增值税 编辑:程序博客网 时间:2024/05/18 00:21
1>. 介绍Android驱动
<1. Android驱动 = Linux驱动 + 封装
<2. 封装指的是 : 服务,硬件访问服务
2>. 第一个Android驱动程序 编写!
<1. 功能:通过几个按钮来控制开发板上的LCD灯
<2. 界面:一个大的总按钮,四个小的子按钮
<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>
- 1.编写第1个Android应用程序实现按钮和复选框
- 1.编写第1个Android应用程序实现按钮和复选框
- 硬件访问服务1之编写第1个Android应用程序实现按钮和复选框
- Android开发之编写第一个Android应用程序实现按钮和复选框
- 第9章 反转按钮、复选框和单选框
- 第11章、单选按钮和复选框
- 编写Android程序,实现选中复选框后,“开始”按钮才可用,否则为不可用状态
- 按钮和复选框控件
- Android 单选按钮和复选框事例
- Android-单选按钮RadioButton和复选框checkbox
- Android中的 单选按钮和复选框
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- 《Java 2 图形设计卷Ⅱ- SWING》第9章 反转按钮、复选框和单选框
- 复选框和删除按钮综合
- 2复选框和单选按钮
- (能直接用!)网络监测广播且能自动更新状态,跟着Activity生存周期运作
- ios中多线程的使用
- WebSphere MQ 一些常用的命令
- 时间与时间戳的转换
- canvas 绘制动态圆环进度条
- 1.编写第1个Android应用程序实现按钮和复选框
- 如何设置EditText为密码框,并且得到焦点时弹出的是数字键盘
- 编程领域内为什么要有那么多的语言?
- 数据结构头结点循环链表
- Timeline Maker Pro 4.2.39 最新版 中文 汉化版 支持 简 / 繁 windows 系统 最好的时间轴图表制作工具
- SpringMVC默认欢迎页面的问题
- PAT1036.跟奥巴马一起编程
- JDK源码解析之HashSet
- JAVA 扩展(待整理)