20169219 实验四Android程序设计

一、实现Linux下dc的功能,计算后缀表达式的值

public int evaluate(String expr) {
        int op1, op2, result = 0;
        String token;
        StringTokenizer tokenizer = new StringTokenizer(expr);

        while (tokenizer.hasMoreTokens()) {
            token = tokenizer.nextToken();

    //如果是运算符,调用isOperator
            if (isOperator(token)) {
                //从栈中弹出操作数2
                op2 = (stack.pop()).intValue();
                //从栈中弹出操作数1
                op1 = (stack.pop()).intValue();
                //根据运算符和两个操作数调用evalSingleOp计算result;
                result = evalSingleOp(token.charAt(0), op1, op2);
                //计算result入栈;
                stack.push(new Integer(result));
            } else {//如果是操作数
                //操作数入栈;
                stack.push(new Integer(Integer.parseInt(token)));
            }
        }
        return result;
    }

二、完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号

三、修改代码让MainActivity启动ThirdActivity

    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
        Intent intent = new Intent(this, ThirdActivity.class);
        intent.putExtra("message", "Message from First Screen");
        startActivity(intent);
        return true;
    }

为了触碰事件,MainActivity类实现OnTouchListener接口。

四、修改代码让Toast消息中显示自己的学号信息

在Android开发中,我们经常会遇到需要给用户一个提示,但是又不想影响整体的体验,那么Toast出现了。Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。而且Toast主要用于向用户显示提示消息。

  • 传参介绍

Toast.makeText(Context context, CharSequence text, @Duration int duration)

  • contentx : 当前的上下文环境。可用getApplicationContext()或this
  • text : 要显示的字符串。
  • duration : 显示的时间长短,以毫秒为单位。Toast默认的有两个LENGTH_LONG(长)和LENGTH_SHORT(短),也可以使用毫秒如2000

显示的几种方式

1、默认
2、自定义位置
3、带图片
4、完全自定义
完全自定义页面

   <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:id="@+id/imageView"
    />
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/textView"
              android:layout_below="@id/imageView"
    />

五、修改布局让P290页的界面与教材不同

LinearLayout 线性布局
FrameLayout 单帧布局,也有中文翻译为帧布局、框架布局。
RelativeLayout 相对布局
AbsoluteLayout 绝对布局
TableLayout 表格布局

每种布局都有一行重要的代码来设置:

LinearLayout 中的 android:orientation="vertical"

详细使用方法可参考Android 开发之旅:view的几种布局方式及实践

六、监听器的使用

Android提供的基于事件监听接口有OnClickListener、OnLongClickListener、OnFocusChangeListener、OnKeyListener、OnTouchListener、OnCreateContextMenuListener等。

1)OnClickListener接口:该接口处理的是点击事件。在触摸模式下,是在某个View上按下并抬起的组合动作,而在键盘模式下,是某个View获得焦点后点击确定键或者按下轨迹球事件。
2)OnLongClickListener接口: OnLongClickListener接口与上述的OnClickListener接口原理基本相同,只是该接口为View长按事件的捕捉接口,即当长时间按下某个View时触发的事件。
3)OnFocusChangeListener接口:OnFocusChangeListener接口用来处理控件焦点发生改变的事件。如果注册了该接口,当某个控件失去焦点或者获得焦点时都会触发该接口中的回调方法。
4)OnKeyListener接口:是对手机键盘进行监听的接口,通过对某个View注册并监听,当View获得焦点并有键盘事件时,便会触发该接口中的回调方法。
5)OnTouchListener接口:是用来处理手机屏幕事件的监听接口,当为View的范围内触摸按下、抬起或滑动等动作时都会触发该事件。
6)OnCreateContextMenuListener接口:是用来处理上下文菜单显示事件的监听接口。该方法是定义和注册上下文菜单的另一种方式。

public class MainActivity extends Activity {

    int counter = 0;
    int[] colors = {Color.BLACK, Color.BLUE, Color.CYAN,
            Color.DKGRAY, Color.GRAY, Color.GREEN, Color.LTGRAY,
            Color.MAGENTA, Color.RED, Color.WHITE, Color.YELLOW};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @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;
    }

    public void changeColor(View view) {
        if (counter == colors.length) {
            counter = 0;
        }
        view.setBackgroundColor(colors[counter++]);
    }
}

七、简易计算器
参考了一个android开发视频编写计算器。

switch (opt){
                case Types.ADD:
                    items.add(new Item(a+b,Types.NUM));
                    break;
                case Types.SUB:
                    items.add(new Item(a-b,Types.NUM));
                    break;
                case Types.X:
                    items.add(new Item(a*b,Types.NUM));
                    break;
                case Types.DIV:
                    items.add(new Item(a/b,Types.NUM));
                    break;
            }

出现的问题

1、测试MyDCTest总是出错,显示报错信息是0 test classes found in package 'exp4'
检查多次发现测试类的名称有错误,之前写的是MyDCTest,需要改成MyDCTester

2、报错“no resource identifier found for attribute 'weight' in package 'android'”
代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

......

<Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weight="1"
            android:text="1"/>

解决方法:在android:weight="1"处应该改为android:layout_weight="1",同时,layout_weight只适用于LinearLayout

参考资料

Android 开发之旅:view的几种布局方式及实践

Android 中 Toast用法

posted @ 2017-05-07 20:44  毛卫华  阅读(245)  评论(1编辑  收藏  举报