在原有应用中,还不能让用户自己添加茶叶,及修改每种茶叶的泡茶时间等等。
下面我们将完成这些功能。
首先要以一个菜单来在让用户执行这些功能,主要有添加及修改相关功能。
一般在android机都在机身了提供了一个menu按钮。当用户点击机身上的"Menu"按钮时,选项工菜单一般设置在底部出现。
Android会自己负责菜单的自动创建和显示。我们只需要告诉android。菜单显示什么内容及当用户点击相应的菜单选项时该做什么就行了。
先在string.xml文件中添加一个菜单标签先。如下:
    <!--  begin Menu 选项菜单项  -->    <string name="add_tea_label">添加茶</string>    <!--  end Menu 选项菜单项  -->


创建选项菜单:
创建新Android Xml File。然后文件名为:main.xml
资源类型为menu。然后输入目录。eclipse自己会创建一个res/menu目录的。
打开res/menu/main.xml文件,添加菜单项。main.xml文件如下:
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id="@+id/add_tea" android:title="@string/add_tea_label"></item></menu>



然后在BrewClockActivity.java代码中重载onCreateOptionsMenu()这个方法。
这个方法告诉Android,当用户点击"Menu"按钮时装载我们的菜单。
@Overridepublic boolean onCreateOptionsMenu(Menu menu){MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.main, menu);return true;}


当用户点击"Menu"按钮时Android将调用onCreateOptionsMenu()方法。
inflate是充气膨胀膨化的意思。
MenuInflater类用来将menu的xml文件实例化为菜单对象。
引用
This class is used to instantiate menu XML files into Menu objects.

在Activity类中有用来返回MenuInflater对象的方法。
public MenuInflater getMenuInflater ()Since: API Level 1Returns a MenuInflater with this context.


小插曲,我现有时候模拟器运行不起来,我就跑到终端将其杀死。但是android在运行模拟器的同时也创建了其他进程,所以当我删除一个avd的时候,提示无法删除啊。
然后我新建一个,新建一个第一次运行时比较慢。
然后在run dialog..处还要确定target是新建的avd。

当程序运行起来的时候点击"Menu"按键时会在窗口在底部弹出一个菜单。
点击后菜单自动消失。
下面来处理下菜单点击事件。

当用户点击添加菜单后,菜单消失了,但是我们此时应该给用户一个添加菜叶品种记录的添加界面。也就是一个Activity子类。

如下:
package me.banxi.brewclock;import android.app.Activity;import android.os.Bundle;public class AddTeaActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}}


然后再将这个activity注册到应用程序中。即在AndroidManifest.xml中的Application节点下
添加一个声明Activity的节点。
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="me.banxi.brewclock"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".BrewClockActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=".AddTeaActivity" android:label="@string/add_tea_label" />    </application></manifest>


接下来开发茶叶添加编辑页面。
新建一个Android Xml 文件。选择资源类型为layout。
命名为add_tea.xml。
如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">    <TextView android:text="@string/tea_name_label"    android:textSize="30dip"              android:layout_width="match_parent"              android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1"/>    <EditText android:id="@+id/tea_name"    android:layout_width="fill_parent"    android:layout_height="wrap_content" android:layout_weight="1"/>    <TextView android:text="@string/brew_time_label"    android:textSize="30dip"    android:layout_width="fill_parent"    android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1"/>    <SeekBar  android:id="@+id/brew_time_seekbar"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:progress="2"    android:max="9" android:layout_weight="1"/>    <TextView android:id="@+id/brew_time_value"    android:text="3 m"    android:textSize="30dip"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:gravity="center_horizontal" android:layout_weight="1"/>    <LinearLayout android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="wrap_content" android:layout_weight="1">        <Button android:text="@string/ok"           android:id="@+id/ok_add_tea"            android:layout_height="match_parent"           android:layout_weight="1"           android:layout_margin="@dimen/m9"></Button>        <Button android:text="@string/cancel"                 android:id="@+id/cancel_add_tea"                  android:layout_height="match_parent"                 android:layout_weight="1"                 android:layout_margin="@dimen/m9"></Button>        </LinearLayout></LinearLayout>


值得注意的是上面的让两个按钮并排显示。的代码。
上面有一个新的东西就是"@dimen/m9"。其实跟"@string/name"类似的。
有一个dimens.xml的文件在values目录下。
res/values/dimens.xml
<?xml version="1.0" encoding="utf-8"?><resources>    <dimen name="m7">7dip</dimen>    <dimen name="m9">9dip</dimen></resources>


在运行时发再没有画面,仔细查看了下:
发现应该还要把AddTeaActivity的onCreate()方法修改如下:
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.add_tea);}


运行的时候遇到下面的一个错误:
引用
11-10 01:40:03.609: ERROR/AndroidRuntime(711): Caused by: java.lang.RuntimeException: Binary XML file line #30: You must supply a layout_width attribute.


检查后发现,因为button中没有layout_width添加之后。问题解决:
    <LinearLayout android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="wrap_content" android:layout_weight="1">        <Button android:text="@string/ok"           android:id="@+id/ok_add_tea"            android:layout_height="match_parent"          android:layout_width="match_parent"           android:layout_weight="1"           android:layout_margin="@dimen/m9"></Button>        <Button android:text="@string/cancel"                 android:id="@+id/cancel_add_tea"                  android:layout_height="match_parent"                android:layout_width="match_parent"                  android:layout_weight="1"                 android:layout_margin="@dimen/m9"></Button>        </LinearLayout>


下面在AddTeaActivity中关联界面表单属性。
及设置相关事件,处理事件。如下,一个相对完整的AddTeaActivity类了
package me.banxi.brewclock;import android.app.Activity;import android.app.AlertDialog;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.TextView;import android.widget.Toast;public class AddTeaActivity extends Activity {protected EditText teaName;protected SeekBar brewTimeSeekBar;protected TextView brewTimeLabel;protected Button okButton;protected Button cancelButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.add_tea);// get form view object from xmlteaName = (EditText) findViewById(R.id.tea_name);brewTimeSeekBar = (SeekBar)findViewById(R.id.brew_time_seekbar);brewTimeLabel = (TextView)findViewById(R.id.brew_time_value);okButton = (Button)findViewById(R.id.ok_add_tea);cancelButton = (Button)findViewById(R.id.cancel_add_tea);// 为SeekBar添加事件监听器brewTimeSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {   public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {if(seekBar == brewTimeSeekBar){brewTimeLabel.setText((progress+1)+"m");}}public void onStopTrackingTouch(SeekBar seekBar) {}public void onStartTrackingTouch(SeekBar seekBar) {}});// 为OK Button 添加事件监听器okButton.setOnClickListener(new OnClickListener() {public void onClick(View v) {String teaNameText = teaName.getText().toString().trim();int brewTimeValue = brewTimeSeekBar.getProgress()+1;if(teaNameText.length() < 2){AlertDialog.Builderdialog = new AlertDialog.Builder(getApplicationContext());dialog.setTitle(R.string.invalid_tea_name);dialog.setMessage(R.string.empty_tea_name);dialog.show();}else{saveTea(teaNameText, brewTimeValue);}}});}// onCrreateprivate void saveTea(String teaNameText, int brewTimeValue) {TeasOpenHelper teasOpenHelper = new TeasOpenHelper(this);teasOpenHelper.insert(teaNameText, brewTimeValue);teasOpenHelper.close();Toast.makeText(getApplicationContext(), getString(R.string.tea_add_success),Toast.LENGTH_SHORT).show();}}


一个值得注意的地方就是,上面的弹出的AlertDialog要用Back键才能消除。因为没有设置确定按钮。

总结下上面用到的新控件就是:
(1) SeekBar。相当于一个进度条。
(2) AlertDailog。有点类似于JavaScript中的alert()。
(3)Toast异步弹出消息框。

在模拟器玩android时,我发现,有两点问题。
(0) 添加成功之后,要重新启动这个程序才能显示新添加的茶叶。(等改进)
(1)不能输入中文。
(2) 不知道怎么上传音乐文件来测试音乐播放。

上面把主要的介绍到这里了,整个项目文件在附件里。
后面还会有完善的代码放出。
从开始接触到跟着教程和自己理解做成了这个一个小程序。
嗯,以后接着努力。
未完待续------------

更多相关文章

  1. Android Things:用户驱动-传感器
  2. android解析XML文件的三方法之SAX
  3. android 标题栏下拉选择控件(下拉菜单宽度全屏显示spinner)
  4. android文件下载与保存
  5. android 扫描文件(sdcard添加新的音乐文件时候后,可扫描到)
  6. Android stutio 中怎么将XML中文件快速findById——Android Layo

随机推荐

  1. 当开始使用数据-*时,无法识别AngularJS指
  2. 如何禁用Cell Handsontable中的写入并包
  3. ajax交互Struts2的action(1)
  4. 如何设置动画以使元素围绕圆圈移动?
  5. 通过切换a. localecompare (b) to (ab?1:
  6. 在Angular服务中使用$ http作为json数据
  7. JSON.parse(xhr.responseText)意外的字符串
  8. [jQuery知识]jQuery之知识四-DOM和CSS操
  9. 与jQuery ajax post方法相比,fetch方法没
  10. JavaScript中可见性检查的测试条件