孙星

20169221 2016-2017-2 《移动平台应用与开发》第十周实验总结

实验内容

课堂测试:提交测试截图和码云练习项目链接,实现Linux下dc的功能,计算后缀表达式的值

1.后缀表达式的计算规则:
后缀表达式的计算过程:从左到右扫描整个表达式,把每个操作符应用到其之前两个相邻的操作数,并计算该结果,剩下的表达式以此类推。
1+23
可以转换成后缀表达式如下:
1 2 3 * +
计算过程如下:
1 2
3 + --> 1 6 + -->7
程序设计的思路:

  • 从左到右扫描表达式,以此标识出每个符号(操作数或操作符)。如果是操作数,
  • 则把它压入栈中。如果是操作符,则从栈中弹出两个元素,
  • 并把该操作符应用在这两个元素只上,然后把操作结果压入栈中。
  • 当到达表达式的末尾时,栈中所神域的元素就是该表达式的计算结果。
    MyDC.java代码
/**
 * Created by Administrator on 2017/5/4 0004.
 */
import java.util.StringTokenizer;
import java.util.Stack;

public class MyDC {
    /**
     * constant for addition symbol
     */
    private final char ADD = '+';
    /**
     * constant for subtraction symbol
     */
    private final char SUBTRACT = '-';
    /**
     * constant for multiplication symbol
     */
    private final char MULTIPLY = '*';
    /**
     * constant for division symbol
     */
    private final char DIVIDE = '/';
    /**
     * the stack
     */
    private Stack<Integer> stack;

    public MyDC() {
        stack = new Stack<Integer>();
    }

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

    private boolean isOperator(String token) {
        return (token.equals("+") || token.equals("-") ||
                token.equals("*") || token.equals("/"));
    }

    private int evalSingleOp(char operation, int op1, int op2) {
        int result = 0;

        switch (operation) {
            case ADD:
                result = op1 + op2;
                break;
            case SUBTRACT:
                result = op1 - op2;
                break;
            case MULTIPLY:
                result = op1 * op2;
                break;
            case DIVIDE:
                result = op1 / op2;
        }

        return result;
    }
}


myDCTest代码:

import junit.framework.TestCase;

import java.util.Scanner;
import junit.framework.TestCase;


    public class MyDCTester  {

        public static void main (String[] args) {

            String expression, again;

            int result;

            try
            {
                Scanner in = new Scanner(System.in);

                do
                {
                    MyDC evaluator = new MyDC();
                    System.out.println ("Enter a valid postfix expression: ");
                    expression = in.nextLine();

                    result = evaluator.evaluate (expression);
                    System.out.println();
                    System.out.println ("That expression equals " + result);

                    System.out.print ("Evaluate another expression [Y/N]? ");
                    again = in.nextLine();
                    System.out.println();
                }
                while (again.equalsIgnoreCase("y"));
            }
            catch (Exception IOException)
            {
                System.out.println("Input exception reported");
            }
        }
    }

简易计算器代码

import java.awt.*;
import java.awt.event.*;
import java.util.Stack;
public class Calculator implements ActionListener,MouseListener{
 private Frame frame;
 
 private Panel panel;
 private Label memorFlag=new Label();
 private double memor;
 private TextArea texpress;
 private boolean expressLegal=true;//检测表达式是否合法
 private Label lresult=new Label("0",Label.RIGHT);
 
 private Button btmemor[]=new Button[4];//0 MC, 1 MR, 2 M+ , 3 M-
 private Button []btarray=new Button[12];
 private Button add=new Button("+");
 private Button sub=new Button("-");
 private Button mul=new Button("*");
 private Button div=new Button("/");
 private Button clean=new Button("C");
 private Button backspace=new Button("←");
 private Button leftbrack=new Button("(");
 private Button rightbrack=new Button(")");
 private Font btFont=new Font("Roman",5,20);
 private Font tFont=new Font("Roman",1,16);
 public Calculator(){
  int i=0;
  //////////////////////  界  面  设  计   ///////////////////////
  frame=new Frame("计算器");
  frame.setLayout(null);
  frame.setVisible(true);
  frame.setLocation(400, 200);
  frame.setSize(375, 470);
  frame.setResizable(false);
  frame.setBackground(new Color(217,228,241));
  
  memorFlag.setSize(320, 30);
  memorFlag.setFont(tFont);
  memorFlag.setBackground(new Color(248,251,254));
  memorFlag.setLocation(25, 42);
  texpress=new TextArea("0",3,20,TextArea.SCROLLBARS_NONE);
  texpress.setSize(320, 60);
  texpress.setBackground(new Color(248,251,254));
  texpress.setFont(tFont);
  texpress.setLocation(25, 70);
  texpress.setCaretPosition(1);
  lresult.setSize(320, 30);
  lresult.setFont(tFont);
  lresult.setBackground(new Color(251,251,254));
  lresult.setLocation(25, 131);
  
  panel=new Panel();
  panel.setLayout(new GridLayout(6,4));
  panel.setSize(320, 240);
  panel.setLocation(25, 185);
  panel.setBackground(new Color(217,228,241));
  for(i=0;i<12;i++){
   btarray[i]=new Button(String.valueOf(i));
   btarray[i].setBackground(new Color(243,248,252));
   btarray[i].setForeground(new Color(30,57,91));
   //btarr[i].setSize(50,60);
   btarray[i].setFont(btFont);
  }
  btarray[10].setLabel(".");
  btarray[11].setLabel("=");
  for(i=0;i<4;i++){
   btmemor[i]=new Button();
   btmemor[i].setBackground(new Color(243,248,252));
   btmemor[i].setForeground(new Color(30,57,91));
   //btarr[i].setSize(50,60);
   btmemor[i].setFont(new Font("Roman",5,13));
   panel.add(btmemor[i]);
  }
  btmemor[0].setLabel("MC");
  btmemor[1].setLabel("MR");
  btmemor[2].setLabel("M+");
  btmemor[3].setLabel("M-");
  leftbrack.setBackground(new Color(243,248,252));
  leftbrack.setForeground(new Color(30,57,91));
  leftbrack.setFont(btFont);
  rightbrack.setBackground(new Color(243,248,252));
  rightbrack.setForeground(new Color(30,57,91));
  rightbrack.setFont(btFont);
  clean.setBackground(new Color(243,248,252));
  clean.setForeground(new Color(30,57,91));
  clean.setFont(btFont);
  backspace.setBackground(new Color(243,248,252));
  backspace.setForeground(new Color(30,57,91));
  backspace.setFont(btFont);
  add.setBackground(new Color(243,248,252));
  add.setForeground(new Color(30,57,91));
  add.setFont(btFont);
  sub.setBackground(new Color(243,248,252));
  sub.setForeground(new Color(30,57,91));
  sub.setFont(btFont);
  mul.setBackground(new Color(243,248,252));
  mul.setForeground(new Color(30,57,91));
  mul.setFont(btFont);
  div.setBackground(new Color(243,248,252));
  div.setForeground(new Color(30,57,91));
  div.setFont(btFont);
  
  panel.add(backspace);
  panel.add(clean);
  panel.add(leftbrack);
  panel.add(rightbrack);
  panel.add(btarray[7]);
  panel.add(btarray[8]);
  panel.add(btarray[9]);
  panel.add(div);
  panel.add(btarray[4]);
  panel.add(btarray[5]);
  panel.add(btarray[6]);
  panel.add(mul);
  panel.add(btarray[1]);
  panel.add(btarray[2]);
  panel.add(btarray[3]);
  panel.add(sub);
  panel.add(btarray[0]);
  panel.add(btarray[10]);
  panel.add(btarray[11]);
  panel.add(add);
  
  frame.add(memorFlag);
  frame.add(lresult);
  frame.add(texpress);
  frame.add(panel);
  /////////////////////////为组件添加事件///////////////////////
  frame.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e){
    System.exit(0);
   }
  });
  //texpress.
  texpress.addKeyListener(new KeyAdapter(){
   public void keyTyped(KeyEvent e){
    String str=texpress.getText();
    int pos=texpress.getCaretPosition();
    int i=0;
    double resul=0;
    String behindExpress=null;
    char ch=e.getKeyChar();
    if(ch==KeyEvent.VK_ENTER){//按键=或回车
         i=str.indexOf("\n");
         texpress.setText(str.substring(0, i-1)+str.substring(i+1, str.length()));
         texpress.setCaretPosition(pos);
           try{
            behindExpress=toBehindExpress();
            resul=getResult(behindExpress);
           }catch(Exception e2){
         expressLegal=false;
         lresult.setText("ERROR");
           }//catch
           if(expressLegal)
            lresult.setText(normalDouble(resul));
              else
            expressLegal=true;
    }//if
   }//public
  });
  for(i=0;i<4;i++){
   btmemor[i].addActionListener(this);
   btmemor[i].addMouseListener(this);
  }
  for(i=0;i<12;i++){
   btarray[i].addActionListener(this);
   btarray[i].addMouseListener(this);
  }
  backspace.addActionListener(this);
  backspace.addMouseListener(this);
  clean.addActionListener(this);
  clean.addMouseListener(this);
  leftbrack.addActionListener(this);
  leftbrack.addMouseListener(this);
  rightbrack.addActionListener(this);
  rightbrack.addMouseListener(this);
  add.addActionListener(this);
  add.addMouseListener(this);
  sub.addActionListener(this);
  sub.addMouseListener(this);
  mul.addActionListener(this);
  mul.addMouseListener(this);
  div.addActionListener(this);
  div.addMouseListener(this);
 }
 public String toBehindExpress(){//转换为后缀表达式
  String behindExpress="",middleExpress=texpress.getText(),numStr="";
  Stack s=new Stack();
  int i=0;
  char ch,lb1=' ',lb2=' ',lm1=' ';//lb1表示behindExpress最后一个,lb2表示behindExpress倒数第二个
                                          //lm1表示middleExpress中i的前一位置字符
  while(i
   ch=middleExpress.charAt(i);
   if(behindExpress.length()>=1)
       lb1=behindExpress.charAt(behindExpress.length()-1);
   if(behindExpress.length()>=2)
       lb2=behindExpress.charAt(behindExpress.length()-2);
   if(i>0)
    lm1=middleExpress.charAt(i-1);
   switch(ch){
   case '(':
    if(i!=0&&lm1!='('&&lm1!='+'&&lm1!='-'&&lm1!='*'&&lm1!='/'){
     lresult.setText("ERROR");
     //System.out.println(lm1!='*');
     //System.out.println("??"+lm1);
     expressLegal=false;
     return "";
    }
    s.push('(');
    i++;
    break;
   case ')':
    if(lb1!=' ')
        behindExpress+=' ';
    if(numStr.length()!=0){
     behindExpress+=numStr;
     numStr="";
    }
    while(!s.empty()&&s.peek()!='('){
     if(behindExpress.charAt(behindExpress.length()-1)!=' ')
      behindExpress+=' ';
     behindExpress+=s.pop();
    }
    s.pop();//右括号出栈
    i++;
    break;
   case '0':case '1':case '2':case '3':case '4':case'5':case'6':case '7':case '8':case '9':case '.':
    if(lb1!=' ')
     behindExpress+=' ';
    numStr+=ch;
    i++;
    break;
   case '-'://需要考虑负号问题
    if(i==0||lm1=='('||lm1=='+'||lm1=='-'||lm1=='*'||lm1=='/'){
     numStr+=ch;
     i++;
     break;
    }
   case '+':
   case '*':
   case '/':
    if(lb1!=' ')
        behindExpress+=' ';
    if(numStr.length()!=0){
     behindExpress+=numStr;
     numStr="";
    }
    while(!s.empty()&&priorityLevel(s.peek())>=priorityLevel(ch)){
     if(behindExpress.charAt(behindExpress.length()-1)!=' ')
        behindExpress+=' ';
     behindExpress+=s.pop();
    }
    s.push(middleExpress.charAt(i));
    i++;
    break;
   case ' ':i++;break;
   default :expressLegal=false;lresult.setText("ERROR");return "";    
   }//switch
   
  }//for
  if(numStr.length()!=0){
   if(behindExpress.length()!=0&&behindExpress.charAt(behindExpress.length()-1)!=' ')
       behindExpress+=' ';
   behindExpress+=numStr;
  }
  while(!s.empty()){
   if(behindExpress.charAt(behindExpress.length()-1)!=' ')
    behindExpress+=' ';
   behindExpress+=s.pop();
  }
  behindExpress+=' ';
  //System.out.println(middleExpress);
  //System.out.println(behindExpress);
  //System.out.println(behindExpress+"后   "+i);
  return behindExpress;
 }
 public int priorityLevel(char ch){//'('优先级最低,')'优先级最高+、-一个优先级  *、/一个
  switch(ch){
  case '(': return 1;
  case '+':
  case '-': return 2;
  case '*':
  case '/': return 3;
  case ')': return 4;
  default : return -1;
  }
 }
 
 public double getResult(String express){//由后缀表达式计算结果
  double num1,num2;
  int i=0;
  char ch;
  String temp="";
  Stack s=new Stack();
  if(expressLegal==false)
   return 0;
  for(i=0;i
   ch=express.charAt(i);
   if(ch>='0'&&ch<='9'||ch=='.'||(ch=='-'&&(i+1)
    temp+=ch;
    continue;
   }
   switch(ch){
   case ' ':
    if(temp.length()!=0){
     s.push(Double.parseDouble(temp));
     temp="";
    }
    break;
   case '+':case '-':case '*':case '/':
    num2=s.pop();
    num1=s.pop();
    if(ch=='+')  s.push(num1+num2);
    if(ch=='-')  s.push(num1-num2);
    if(ch=='*')  s.push(num1*num2);
    if(ch=='/')  {
     if(num2==0){
      lresult.setText("除数不能为零");
      expressLegal=false;
     }
     s.push(num1/num2);
    }
    break;
   }//switch()
  }//for
  return s.pop();
 }
 public String normalDouble(double num){
  if((Math.round(num)-num)==0)
   return String.valueOf((long)num);
  return String.valueOf(num);
 }
 @Override
 public void actionPerformed(ActionEvent e) {
  int pos=texpress.getCaretPosition();
  String str=texpress.getText();
  Button e1=(Button)e.getSource();
  if(str.equals("0"))
    str="";
  
  if(e1==clean){
   str="";
   lresult.setText("0");
  }
  else if(e1==backspace){
   if(str.length()!=0&&pos!=0){
    str=str.substring(0, pos-1)+str.substring(pos,str.length());
    pos--;
   }
  }
  else if(e1==btmemor[0]){//清除memory
   memorFlag.setText("");
   memor=0;
  }
  else if(e1==btmemor[1]){//提取memory
   str=normalDouble(memor);
   lresult.setText("0");
  }
  else if(e1==btmemor[2]){//M+
   try{
       memor+=Double.parseDouble(lresult.getText());
   }catch(Exception ex){}
   if(memor!=0)
    memorFlag.setText("M    "+normalDouble(memor));
   else
    memorFlag.setText("");
  }
  else if(e1==btmemor[3]){//M-
   try{
       memor-=Double.parseDouble(lresult.getText());
   }catch(Exception ex){}
   if(memor!=0)
    memorFlag.setText("M    "+normalDouble(memor));
   else
    memorFlag.setText("");
  }
  else if(e1==btarray[10]){//小数点
   if(str.length()==0)
    str+="0.";
   else if(str.charAt(str.length()-1)!='.')
    str+=".";
   pos++;
  }
  else if(e1==btarray[11]){//等号
   double resul=0;
   String behindExpress=null;
   try{
   behindExpress=toBehindExpress();
   resul=getResult(behindExpress);
   }catch(Exception e2){
    expressLegal=false;lresult.setText("ERROR");
    }
   if(expressLegal)
       lresult.setText(normalDouble(resul));
   else
    expressLegal=true;
  }
  else{
   if(str=="")
       str=e1.getLabel();
   else if(str.length()==pos)
    str=str.substring(0,pos)+e1.getLabel();
   else
    str=str.substring(0,pos)+e1.getLabel()+str.substring(pos,str.length());
   pos++;
  }
  if(str.length()!=0)
      texpress.setText(str);
  else
   texpress.setText("0");
  //texpress.setFocusable(true);
  texpress.requestFocus();
  texpress.setCaretPosition(pos);
  
 }
 @Override
 public void mouseClicked(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO Auto-generated method stub
  Button e1=(Button)e.getSource();
  e1.setBackground(new Color(255,230,123));
 }
 @Override
 public void mouseExited(MouseEvent e) {
  // TODO Auto-generated method stub
  Button e1=(Button)e.getSource();
  e1.setBackground(new Color(243,248,252));
 }
 @Override
 public void mousePressed(MouseEvent e) {
  // TODO Auto-generated method stub
  Button e1=(Button)e.getSource();
  e1.setBackground(new Color(245,202,126));
 }
 @Override
 public void mouseReleased(MouseEvent e) {
  // TODO Auto-generated method stub
  Button e1=(Button)e.getSource();
  e1.setBackground(new Color(255,230,123));
 }
}

简易计算器测试代码

package Test;
public class CalculatorTest {
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Calculator ct= new Calculator();
 }
}

实验结果

实验一 第一个Android Studio程序

Android Stuidio的安装测试: 参考《Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)》第二十四章:

  • 安装 Android Stuidio
  • 完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号,提交代码运行截图和码云Git链接,截图没有学号要扣分
  • 学习Android Stuidio调试应用程序
    1.新建一个 new Android Studio project
    2.选择最小版本的SDK
    3.选择“Blank Activity”
    4.输入信息布局后,完成创建
    5.运行helloworld代码

实验二 Activity测试

Activity测试: 参考《Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)》第二十五章:

  • 构建项目,运行教材相关代码
  • 创建 ThirdActivity, 在ThirdActivity中显示自己的学号,修改代码让MainActivity启动ThirdActivity
  • 提交代码运行截图和码云Git链接,截图要有学号水印,否则会扣分
    1.新建一个LayOut XML File
    2.修改代码activity_second
    3.新建一个ThirdActivity
    4.新建Activity成功了,在主界面添加一个“按钮”用于跳转Activity,修改action_main.xml文件
    5.添加函数onClick

实验三 Android Studio 在活动中使用Toast

UI测试: 参考《Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)》第二十六章:

  • 构建项目,运行教材相关代码
  • 修改代码让Toast消息中显示自己的学号信息
  • 提交代码运行截图和码云Git链接,截图要有学号水印,否则会扣分
    1.Toast是一种很方便的消息提示框,会在 屏幕中显示一个消息提示框,没任何按钮,也不会获得焦点、一段时间过后自动消失!
    2.特点:
    Toast是一种提供给用户简洁提示信息的视图。
    不能获得焦点
    显示一段时间后自动消失
    Toast 是一个 View 视图,快速的为用户显示少量的信息。
    不影响用户的输入等操作,主要用于 一些帮助 / 提示。
    Toast.makeText(Mainthis, “提示的内容”, Toast.LENGTH_SHORT).show(); 第一个是上下文对象!对二个是显示的内容!第三个是显示的时间,只有LONG和SHORT两种 会生效,即时你定义了其他的值,最后调用的还是这两个!

实验四

布局测试: 参考《Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)》第二十七章:

  • 构建项目,运行教材相关代码
  • 修改布局让P290页的界面与教材不同
  • 提交代码运行截图和码云Git链接,截图要有学号水印,否则会扣分

实验五 监听器

事件处理测试: 参考《Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)》第二十八章:

  • 构建项目,运行教材相关代码
  • 提交代码运行截图和码云Git链接,截图要有学号水印,否则会扣分
    1.监听器的三种实现方法
    方法一:在Activity中定义一个内部类继承监听器接口。或者可以用另外一种方式,即new一个该监听器(OnClickListener)的对象,这个方式与上面的直接继承有异曲同工之妙。以上两个实现的监听器在onCreate(Bundle savedInstanceState)方法中的调用都是一样的,即使用setOnClickListener()方法。
  private View.OnClickListener MyListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,"you have clicked Button2",Toast.LENGTH_SHORT).show();
        }
    };

方法二:实现匿名内部类。这种方法适合只希望对监听器进行一次性使用的情况,在该代码块运行完毕之后,该监听器也就不复存在了。

bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"you have clicked Button1",Toast.LENGTH_SHORT).show();
            }
        });

方法三:利用布局文件中的onClick属性,并在实现文件中实现该方法。注意的是这里的方法名应该和布局文件中onClick属性的方法名相同,该方法必须是public方法。

    public void onButtonClick (View view){
        Toast.makeText(MainActivity.this,"you have clicked Button3",Toast.LENGTH_SHORT).show();
    }
}

布局文件设置

<Button
        android:layout_below="@id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:text="Button3"
        android:onClick="onButtonClick"/>

完整代码
布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.jeffrey.listener.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:text="OnClickListener" />
    <Button
        android:layout_below="@id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:id="@+id/button1"/>
    <Button
        android:layout_below="@id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:id="@+id/button2"/>
    <Button
        android:layout_below="@id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:text="Button3"
        android:onClick="onButtonClick"/>
</RelativeLayout>

实现文件:

package com.example.jeffrey.listener;

import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bt1 = (Button)findViewById(R.id.button1);//对应方法二
        Button bt2 = (Button)findViewById(R.id.button2);//对应方法一

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"you have clicked Button1",Toast.LENGTH_SHORT).show();
            }
        });

        bt2.setOnClickListener(new MyListener());

    }
    class MyListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,"you have clicked Button2",Toast.LENGTH_SHORT).show();
        }
    }
    // 或者,这里是创建一个OnClickListener 的对象,与上面的直接复写接口有异曲同工之妙
    private View.OnClickListener MyListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,"you have clicked Button2",Toast.LENGTH_SHORT).show();
        }
    };

    // 方法三,注意需要public方法
    public void onButtonClick (View view){
        Toast.makeText(MainActivity.this,"you have clicked Button3",Toast.LENGTH_SHORT).show();
    }
}

代码托管

参考资料

Android移动网站开发详解
Android开发学习笔记:5大布局方式详解

posted on 2017-05-07 19:40  20169221孙星  阅读(533)  评论(0编辑  收藏  举报

导航