Android实验格式简易计算器实验报告

实验报告

2013-2014 学年第2学期

课程名称:嵌入式操作系统

实验题目:简易计算器的设计与实现

专业:计算机科学与技术、信息处理(是什么专业,写什么专业) 班级:计算本1101(按自己班级填写)

日期填写 20xx年4月 8日

实验题目:简易计算器的设计与实现

实验类型:上机实验

一、实验目的及要求

1. 掌握线性布局的应用;

2. 掌握表格布局的应用;

3. 掌握数组的应用;

4. 掌握如何Button组件的使用;

5. 掌握代码中访问布局组件的方法;

6. 掌握特定组件的事件设计方法。

二、实验仪器设备与软件环境

1、电脑。

2、Android集成开发工具adt-bundle。

三、实验过程

1、布局设计

…这部分自己填

2. 程序设计

…这部分自己填

 

第二篇:Android 简易计算器

Android 简易计算器

20##-04-05 10:34:43     我来说两句      

收藏    我要投稿

安卓版计算器,只实现简单功能,Android 2.2真机测试,

布局采用TableLayout,main.xml代码如下:


[html] <?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="1" 
    android:textSize="32sp" > 
 
    <TableRow> 
        <EditText 
            android:id="@+id/editText" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_span="4" 
            android:cursorVisible="false" 
            android:editable="false" 
            android:gravity="right|center_vertical" 
            android:lines="1" 
            android:textSize="32sp" /> 
    </TableRow> 
     
    <TableRow> 
        <LinearLayout 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_weight="1" 
            android:orientation="horizontal" 
            android:textSize="32sp" > 
            <Button 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:textSize="32sp" /> 
            <Button 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/clear" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:text="C" 
                android:layout_weight="1" 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/add" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="+" 
                android:textSize="32sp" /> 
        </LinearLayout> 
    </TableRow> 
     
    <TableRow> 
        <LinearLayout 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_weight="1" 
            android:orientation="horizontal" 
            android:textSize="32sp" > 
            <Button 
                android:id="@+id/num7" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="7" 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/num8" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="8" 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/num9" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="9" 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/subtract" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="-" 
                android:textSize="32sp" /> 
        </LinearLayout> 
    </TableRow> 
 
    <TableRow> 
        <LinearLayout 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_weight="1" 
            android:orientation="horizontal" 
            android:textSize="32sp" > 
            <Button 
                android:id="@+id/num4" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="4" 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/num5" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="5" 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/num6" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="6" 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/multiply" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="*" 
                android:textSize="32sp" /> 
        </LinearLayout> 
    </TableRow> 
 
    <TableRow> 
        <LinearLayout 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_weight="1" 
            android:orientation="horizontal" 
            android:textSize="32sp" > 
            <Button 
                android:id="@+id/num1" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="1" 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/num2" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="2" 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/num3" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="3" 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/divide" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="/" 
                android:textSize="32sp" /> 
        </LinearLayout> 
    </TableRow> 
 
    <TableRow> 
        <LinearLayout 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_weight="1" 
            android:orientation="horizontal" 
            android:textSize="32sp" > 
            <Button 
                android:id="@+id/num0" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="0" 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/point" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="." 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/negative" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="-" 
                android:textSize="32sp" /> 
            <Button 
                android:id="@+id/equal" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:text="=" 
                android:textSize="32sp" /> 
        </LinearLayout> 
    </TableRow> 
 
</TableLayout> 
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    android:textSize="32sp" >

    <TableRow>
        <EditText
            android:id="@+id/editText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="4"
            android:cursorVisible="false"
            android:editable="false"
            android:gravity="right|center_vertical"
            android:lines="1"
            android:textSize="32sp" />
    </TableRow>
   
    <TableRow>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:textSize="32sp" >
            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="32sp" />
            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="32sp" />
            <Button
                android:id="@+id/clear"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="C"
                android:layout_weight="1"
                android:textSize="32sp" />
            <Button
                android:id="@+id/add"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="+"
                android:textSize="32sp" />
        </LinearLayout>
    </TableRow>
   
    <TableRow>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:textSize="32sp" >
            <Button
                android:id="@+id/num7"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="7"
                android:textSize="32sp" />
            <Button
                android:id="@+id/num8"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="8"
                android:textSize="32sp" />
            <Button
                android:id="@+id/num9"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="9"
                android:textSize="32sp" />
            <Button
                android:id="@+id/subtract"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="-"
                android:textSize="32sp" />
        </LinearLayout>
    </TableRow>

    <TableRow>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:textSize="32sp" >
            <Button
                android:id="@+id/num4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="4"
                android:textSize="32sp" />
            <Button
                android:id="@+id/num5"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="5"
                android:textSize="32sp" />
            <Button
                android:id="@+id/num6"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="6"
                android:textSize="32sp" />
            <Button
                android:id="@+id/multiply"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="*"
                android:textSize="32sp" />
        </LinearLayout>
    </TableRow>

    <TableRow>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:textSize="32sp" >
            <Button
                android:id="@+id/num1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1"
                android:textSize="32sp" />
            <Button
                android:id="@+id/num2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="2"
                android:textSize="32sp" />
            <Button
                android:id="@+id/num3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="3"
                android:textSize="32sp" />
            <Button
                android:id="@+id/divide"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="/"
                android:textSize="32sp" />
        </LinearLayout>
    </TableRow>

    <TableRow>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:textSize="32sp" >
            <Button
                android:id="@+id/num0"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="0"
                android:textSize="32sp" />
            <Button
                android:id="@+id/point"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="."
                android:textSize="32sp" />
            <Button
                android:id="@+id/negative"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="-"
                android:textSize="32sp" />
            <Button
                android:id="@+id/equal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="="
                android:textSize="32sp" />
        </LinearLayout>
    </TableRow>

</TableLayout>
下面是CalculatorActivity.java代码,有详细注释:


[java] public class CalculatorActivity extends Activity implements OnClickListener{ 
    EditText editText; 
    Button num0,num1,num2,num3,num4,num5,num6,num7,num8,num9; 
    Button clear,add,subtract,multiply,divide,point,negative,equal; 
    double firNum=0,secNum=0; 
    //运算符号  
    char c; 
    //小数点控制  
    boolean dot=true; 
     
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        init(); 
    } 
    private void init(){ 
        num0=(Button)findViewById(R.id.num0); 
        num1=(Button)findViewById(R.id.num1); 
        num2=(Button)findViewById(R.id.num2); 
        num3=(Button)findViewById(R.id.num3); 
        num4=(Button)findViewById(R.id.num4); 
        num5=(Button)findViewById(R.id.num5); 
        num6=(Button)findViewById(R.id.num6); 
        num7=(Button)findViewById(R.id.num7); 
        num8=(Button)findViewById(R.id.num8); 
        num9=(Button)findViewById(R.id.num9); 
        clear=(Button)findViewById(R.id.clear); 
        editText=(EditText)findViewById(R.id.editText); 
        add =(Button) findViewById(R.id.add); 
        subtract =(Button) findViewById(R.id.subtract); 
        multiply =(Button) findViewById(R.id.multiply); 
        divide =(Button) findViewById(R.id.divide); 
        point=(Button)findViewById(R.id.point); 
        negative =(Button) findViewById(R.id.negative); 
        equal =(Button) findViewById(R.id.equal); 
        //注册单击事件  
        num0.setOnClickListener(this); 
        num1.setOnClickListener(this); 
        num2.setOnClickListener(this); 
        num3.setOnClickListener(this); 
        num4.setOnClickListener(this); 
        num5.setOnClickListener(this); 
        num6.setOnClickListener(this); 
        num7.setOnClickListener(this); 
        num8.setOnClickListener(this); 
        num9.setOnClickListener(this); 
        add.setOnClickListener(this); 
        clear.setOnClickListener(this); 
        subtract.setOnClickListener(this); 
        multiply.setOnClickListener(this); 
        divide.setOnClickListener(this); 
        point.setOnClickListener(this); 
        negative.setOnClickListener(this); 
        equal.setOnClickListener(this); 
    } 
    //响应单击事件  
    public void onClick(View v) { 
        if(v.equals(num0)){ 
            editText.setText(editText.getText()+"0"); 
        } 
        if(v.equals(num1)){ 
            editText.setText(editText.getText()+"1"); 
        } 
        if(v.equals(num2)){ 
            editText.setText(editText.getText()+"2"); 
        } 
        if(v.equals(num3)){ 
            editText.setText(editText.getText()+"3"); 
        } 
        if(v.equals(num4)){ 
            editText.setText(editText.getText()+"4"); 
        } 
        if(v.equals(num5)){ 
            editText.setText(editText.getText()+"5"); 
        } 
        if(v.equals(num6)){ 
            editText.setText(editText.getText()+"6"); 
        } 
        if(v.equals(num7)){ 
            editText.setText(editText.getText()+"7"); 
        } 
        if(v.equals(num8)){ 
            editText.setText(editText.getText()+"8"); 
        } 
        if(v.equals(num9)){ 
            editText.setText(editText.getText()+"9"); 
        } 
        if(v.equals(clear)){ 
            editText.setText(""); 
        } 
        //如果editText为空  
        if(editText.getText().toString().equalsIgnoreCase("")){ 
            Toast.makeText(this, "没有输入数字!", Toast.LENGTH_SHORT).show(); 
        }else   { 
            if(v.equals(add)){ 
                c='+'; 
                firNum=Double.parseDouble(editText.getText().toString()); 
                editText.setText(""); 
            } 
            if(v.equals(subtract)){ 
                c='-'; 
                firNum=Double.parseDouble(editText.getText().toString()); 
                editText.setText(""); 
            } 
            if(v.equals(multiply)){ 
                c='*'; 
                firNum=Double.parseDouble(editText.getText().toString()); 
                editText.setText(""); 
            } 
            if(v.equals(divide)){ 
                c='/'; 
                firNum=Double.parseDouble(editText.getText().toString()); 
                editText.setText(""); 
            } 
            if(v.equals(point)){ 
                dot=false; 
                editText.setText(editText.getText()+"."); 
            } 
            //等于  
            if(v.equals(equal)){ 
                double result=0; 
                secNum=Double.parseDouble(editText.getText().toString()); 
                if(c=='+'){ 
                    result=firNum+secNum; 
                } 
                if(c=='-'){ 
                    result=firNum-secNum; 
                } 
                if(c=='*'){ 
                    result=firNum*secNum; 
                } 
                if(c=='/'){ 
                    if(secNum==0){ 
                        Toast.makeText(this, "被除数不能为零!", Toast.LENGTH_SHORT).show(); 
                        editText.setText(""); 
                    }else{ 
                        result=firNum/secNum; 
                    } 
                } 
                editText.setText(result+""); 
            } 
        } 
        if(v.equals(negative)){ 
            editText.setText(editText.getText()+"-"); 
        } 
    } 
    //重写onCreateOptionsMenu()方法,创建菜单,  
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        //创建菜单,并设置图标,使用系统提供的图标,  
        menu.add(Menu.NONE, Menu.FIRST + 1, 1, "关于").setIcon(android.R.drawable.ic_dialog_info); 
        menu.add(Menu.NONE, Menu.FIRST + 2, 2, "XX").setIcon(android.R.drawable.ic_lock_idle_alarm); 
        menu.add(Menu.NONE, Menu.FIRST + 3, 2,"退出").setIcon(android.R.drawable.ic_lock_power_off);; 
        // 返回true即显示菜单  
        return true; 
    } 
    @Override 
    //响应菜单单击事件,  
    public boolean onOptionsItemSelected(MenuItem item) { 
        if(item.getItemId()==Menu.FIRST + 1){ 
            //关于  
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);   
            dialog.setTitle("关于"); 
            dialog.setMessage("简易计算器!\nBy:柒少狐\nQQ:569629066"); 
            dialog.show(); 
        } 
        if(item.getItemId()==Menu.FIRST + 1){ 
            Toast.makeText(this, "我那个去,", Toast.LENGTH_SHORT).show(); 
        } 
        if(item.getItemId()==Menu.FIRST + 3){ 
            //退出  
            this.finish(); 
        } 
        return true; 
    } 
}

    

相关推荐