第一个Android程序 -- HelloAndroid
2015-07-09 09:24
567 查看
第一个Android程序 – HelloAndroid
该程序为题库问答题框架
图视层 – activity_hello_android
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="24dp" android:orientation="vertical"> <TextView android:id="@+id/question_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:padding="24dp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp" android:gravity="center_horizontal" android:orientation="horizontal"> <Button android:id="@+id/true_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp" /> <Button android:id="@+id/false_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp"/> </LinearLayout> <Button android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:padding="24dp" /> </LinearLayout>
控制层 – HelloAndroidActivity
package com.example.helloandroid; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class HelloAndroidActivity extends Activity { private Button b1; private Button b2; private Button nb; private TextView qtv; //设置三个按钮变量,一个文本变量 private TrueFalse[] qBank = new TrueFalse[]{ new TrueFalse(R.string.question1,true), //TrueFalse包含两个参数,调用构造函数 new TrueFalse(R.string.question2,false), new TrueFalse(R.string.question3,true), new TrueFalse(R.string.question4,false), };//设置TrueFalse对象数组 private int currentIndex=0; //计数变量 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello_android); //调用名为activity_hello_android的layout的用户界面 qtv = (TextView) findViewById(R.id.question_text_view); //找到id为这个的文本id资源 int question = qBank[currentIndex].getQuestion(); //找到当前问题的文本string资源的id qtv.setText(question); //将数组中的问题显示到qtv上 b1 = (Button) findViewById(R.id.true_button); b1.setText(R.string.true_button); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub //Toast.makeText(HelloAndroidActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show(); /*toast为弹窗消息,生成一个toast用Toast.makeText,三个参数 1.Activity的一个实例 2.toast的string资源id 3.显示时间 Toast.show()显示在屏幕上 */ checkAnswer(true); } }); b2 = (Button) findViewById(R.id.false_button); b2.setText(R.string.false_button); b2.setOnClickListener(new View.OnClickListener() { //设置button2的监听器 @Override public void onClick(View arg0) { // TODO Auto-generated method stub checkAnswer(false); 4000 } }); nb = (Button) findViewById(R.id.next_button); nb.setText(R.string.next_button); //next按钮显示NEXT nb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { currentIndex = (currentIndex+1)%qBank.length; //当前索引先加1 int question = qBank[currentIndex].getQuestion(); //找到当前问题的文本string资源的id qtv.setText(question); //将数组中的问题显示到qtv上 } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.hello_android, menu); return true; } private void checkAnswer(boolean userA){ //判断答案是否正确,参数为用户答案 boolean currans = qBank[currentIndex].isTruequestion(); //currans为正确答案 int maessageId = 0; //maessageId设置toast的string资源id if(userA == currans){ //如果正确 maessageId = R.string.correct_toast; } else{ maessageId = R.string.incorrect_toast; } Toast.makeText(this, maessageId, Toast.LENGTH_SHORT).show(); //显示提示信息 } }
模型层 – TrueFalse
package com.example.helloandroid; public class TrueFalse { private int question;//保存问题的string资源ID private boolean truequestion;//判断是否正确 public TrueFalse(int qt,boolean tqt) {//带参数的构造函数 question=qt; truequestion=tqt; } public int getQuestion() { return question; } public void setQuestion(int question) { this.question = question; } public boolean isTruequestion() { return truequestion; } public void setTruequestion(boolean truequestion) { this.truequestion = truequestion; } }
R.java自动生成
字符串资源 – string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">HelloAndroid</string> <string name="action_settings">Settings</string> <string name="correct_toast">Correct!!!</string> <string name="incorrect_toast">Incorrect!!!</string> <string name="true_button">是</string> <string name="false_button">否</string> <string name="next_button">NEXT</string> <string name="question1">第1个是不是show in English</string> <string name="question2">第2个是不是show in English,不是</string> <string name="question3">第3个是不是show in English</string> <string name="question4">第4个是不是show in English,不是</string> </resources>
改程序包含MVC模式,Android程序资源管理(包括R.java资源ID,和res/下各种资源),页面布局及监听器,Android 少量API的使用
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories