lim的安卓总结

公英制转换

JAVA文件

package com.example.inchmetricconverter;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

 

public class MainActivity extends Activity{

         EditTextedit1;

         EditTextedit2;

 

         @Override

         protectedvoid onCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity_main);

 

                   finalButton Trans1 = (Button) findViewById(R.id.button1);

                   finalButton Trans2 = (Button) findViewById(R.id.button2);

//final EditText  final EditText

                   edit1= (EditText) findViewById(R.id.EditText1);

                   edit2= (EditText) findViewById(R.id.EditText2);

                  

                   Trans1.setOnClickListener(newOnClickListener(){

                            @Override

                            publicvoid onClick(View view) {

                                     //TODO Auto-generated method stub

                                     doubleb = Integer.parseInt(edit1.getText().toString())*2.54;

                                     edit2.setText(String.valueOf(b));

                                     finish();

                            }

                   });

                   Trans2.setOnClickListener(newOnClickListener(){

                            @Override

                            publicvoid onClick(View view) {

                                     //TODO Auto-generated method stub

                                     doublea = Double.parseDouble(edit2.getText().toString())/2.54;

                                     edit1.setText(String.valueOf(a));

                                     finish();

                            }

                   });

         }

}

Main.xml代码:

<RelativeLayoutxmlns: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._inch_metric_converter.MainActivity">

 

 

   <TextView

       android:id="@+id/textView1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignParentTop="true"

       android:layout_marginTop="52dp"

       android:text="英制: " />

   <TextView

       android:id="@+id/textView2"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignBaseline="@+id/EditText2"

       android:layout_alignBottom="@+id/EditText2"

       android:text="公制: " />

 

   <EditText

       android:id="@+id/EditText1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignBaseline="@+id/textView1"

       android:layout_alignBottom="@+id/textView1"

       android:layout_toRightOf="@+id/textView1"

       android:ems="10"

       android:text="" />

 

   <EditText

       android:id="@+id/EditText2"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_below="@+id/EditText1"

       android:layout_marginTop="19dp"

       android:layout_toRightOf="@+id/textView2"

       android:ems="10"

       android:text="" />

 

   <Button

       android:id="@+id/button1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_below="@+id/EditText2"

       android:text="英转公" />

 

   <Button

       android:id="@+id/button2"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_below="@+id/EditText2"

       android:layout_centerHorizontal="true"

       android:text="公转英" />

 

   <TextView

       android:id="@+id/textView3"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignLeft="@+id/EditText1"

       android:layout_alignParentTop="true"

       android:text="英制公制转换器" />

 

</RelativeLayout>

Mune代码:

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

   xmlns:tools="http://schemas.android.com/tools"

   tools:context="com.example.inchmetricconverter.MainActivity">

 

   <item

       android:id="@+id/action_settings"

       android:orderInCategory="100"

        android:showAsAction="never"

       android:title="@string/action_settings"/>

 

</menu>

Dimens代码:

<resources>

 

   <!-- Default screen margins, per the Android Design guidelines.-->

   <dimenname="activity_horizontal_margin">16dp</dimen>

   <dimenname="activity_vertical_margin">16dp</dimen>

 

</resources>

String代码:

<?xml version="1.0"encoding="utf-8"?>

<resources>

 

   <stringname="app_name">InchMetricConverter</string>

   <string name="hello_world">Hello world!</string>

   <string name="action_settings">Settings</string>

 

</resources>

计算器mainacitviity.java

packagecom.example.administrator.mycalculation;

 

importandroid.support.v7.app.AppCompatActivity;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.widget.Button;

importandroid.widget.TextView;

 

publicclass MainActivity extends AppCompatActivity {

 

    TextView rv;

    Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;

    Buttonbad,bsu,bmu,bdi,bpo,bsq,bc,bce,bas,beq;

    @Override

    protected void onCreate(BundlesavedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        rv=(TextView)findViewById(R.id.result);

        b0=(Button)findViewById(R.id.b0);

        b1=(Button)findViewById(R.id.b1);

        b2=(Button)findViewById(R.id.b2);

        b3=(Button)findViewById(R.id.b3);

        b4=(Button)findViewById(R.id.b4);

        b5=(Button)findViewById(R.id.b5);

        b6=(Button)findViewById(R.id.b6);

        b7=(Button)findViewById(R.id.b7);

        b8=(Button)findViewById(R.id.b8);

        b9=(Button)findViewById(R.id.b9);

        beq=(Button)findViewById(R.id.beq);

        bad=(Button)findViewById(R.id.bad);

        bsu=(Button)findViewById(R.id.bsu);

        bdi=(Button)findViewById(R.id.bdi);

        bmu=(Button)findViewById(R.id.bmu);

        bas=(Button)findViewById(R.id.bas);

        bsq=(Button)findViewById(R.id.bsq);

        bpo=(Button)findViewById(R.id.bpo);

        bce=(Button)findViewById(R.id.bce);

        bc=(Button)findViewById(R.id.bc);

 

        Button.OnClickListenerbuttonListener=new Button.OnClickListener(){

            @Override

            public void onClick(View view) {

                switch (view.getId()) {

 

                    case R.id.b0:

                        if (rv.getText().toString().equals("0")){

                           rv.setText(b0.getText());

                        } else {

                           rv.append(b0.getText());

                        }

                        break;

                    case R.id.b1:

                        if(rv.getText().toString().equals("0")) {

                           rv.setText(b1.getText());

                        } else {

                           rv.append(b1.getText());

                        }

                        break;

                    case R.id.b2:

                        if(rv.getText().toString().equals("0")) {

                           rv.setText(b2.getText());

                        } else {

                           rv.append(b2.getText());

                        }

                        break;

                    case R.id.b3:

                        if(rv.getText().toString().equals("0")) {

                           rv.setText(b3.getText());

                        } else {

                           rv.append(b3.getText());

                        }

                        break;

                    case R.id.b4:

                        if(rv.getText().toString().equals("0")) {

                            rv.setText(b4.getText());

                        } else {

                           rv.append(b4.getText());

                        }

                        break;

                    case R.id.b5:

                        if(rv.getText().toString().equals("0")) {

                            rv.setText(b5.getText());

                        } else {

                           rv.append(b5.getText());

                        }

                        break;

                    case R.id.b6:

                        if (rv.getText().toString().equals("0")){

                           rv.setText(b6.getText());

                        } else {

                           rv.append(b6.getText());

                        }

                        break;

                    case R.id.b7:

                        if(rv.getText().toString().equals("0")) {

                           rv.setText(b7.getText());

                        } else {

                           rv.append(b7.getText());

                        }

                        break;

                    case R.id.b8:

                        if(rv.getText().toString().equals("0")) {

                           rv.setText(b8.getText());

                        } else {

                            rv.append(b8.getText());

                        }

                        break;

                    case R.id.b9:

                        if(rv.getText().toString().equals("0")) {

                           rv.setText(b9.getText());

                        } else {

                           rv.append(b9.getText());

                        }

                        break;

                    case R.id.bc:

                       rv.setText("0");

                        break;

                    case R.id.bad:

                       rv.append("+");

                        break;

                    case R.id.bsu:

                       rv.append(bsu.getText());

                        break;

                    case R.id.bmu:

                       rv.append(bmu.getText());

                        break;

                    case R.id.bdi:

                       rv.append(bdi.getText());

                        break;

                    case R.id.bpo:

                        rv.append(bpo.getText());

                        break;

                    case R.id.bsq:

                       rv.setText(bsq.getText());

                        break;

                    case R.id.bce:

                        rv.setText(rv.getText().toString().substring(0,rv.getText().toString().length()-1));

                        break;

                    case R.id.beq:

                        Strings=rv.getText().toString();

                       if(s.equals("0")){

                            rv.setText("0");

                        }

                       if(s.contains("+")){

                            Stringczs[]=s.split("\\+");

                            double res=0.0;

                            int i=0;

                            for(i=0;i<czs.length;i++){

                               res=res+Double.parseDouble(czs[i]);

                            }

                           rv.setText(Double.toString(res));

                        }

                       if(s.contains("-")){

                            Stringczs[]=s.split("-");

                            double res=0.0;

                            int i=0;

                           for(i=0;i<czs.length;i++){

                                if(i==0){

                                    res=Double.parseDouble(czs[i]);

                                }else{

                                   res=res-Double.parseDouble(czs[i]);

                                }

                            }

                            rv.setText(Double.toString(res));

                        }

                       if(s.contains("/")){

                            Stringczs[]=s.split("/");

                            doubleres=Double.parseDouble(czs[0]);

                            int i=0;

                           for(i=0;i<czs.length;i++){

                               if(Double.parseDouble(czs[i])==0){

                                   rv.setText("鏃犳硶璁$畻");

                                   break;

                                }else{

                                   if(i!=0){

                                       res=res/Double.parseDouble(czs[i]);

                                   }

                                }

                            }

                           rv.setText(Double.toString(res));

                        }

                       if(s.contains("*")) {

                            Stringczs[] = s.split("\\*");

                            doubleres = 1.0;

                            int i = 0;

                            for (i =0; i < czs.length; i++) {

                                res= res * Double.parseDouble(czs[i]);

                            }

                           rv.setText(Double.toString(res));

                        }

                       if(s.contains("鈭?)){

                            Stringczs=s.substring(1,s.length());

                            doubleres=Double.parseDouble(czs);

                           res=Math.sqrt(res);

                            rv.setText(Double.toString(res));

                        }

                        break;

                   default:break;

               }

           }

        };

 

       b0.setOnClickListener(buttonListener);

        b1.setOnClickListener(buttonListener);

       b2.setOnClickListener(buttonListener);

       b3.setOnClickListener(buttonListener);

       b4.setOnClickListener(buttonListener);

       b5.setOnClickListener(buttonListener);

       b6.setOnClickListener(buttonListener);

       b7.setOnClickListener(buttonListener);

       b8.setOnClickListener(buttonListener);

       b9.setOnClickListener(buttonListener);

       beq.setOnClickListener(buttonListener);

       bad.setOnClickListener(buttonListener);

        bsu.setOnClickListener(buttonListener);

       bdi.setOnClickListener(buttonListener);

       bmu.setOnClickListener(buttonListener);

       bas.setOnClickListener(buttonListener);

       bpo.setOnClickListener(buttonListener);

        bsq.setOnClickListener(buttonListener);

       bce.setOnClickListener(buttonListener);

       bc.setOnClickListener(buttonListener);

    }

}

 

Main_actiity.xml

<?xmlversion="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:app="http://schemas.android.com/apk/res-auto"

   xmlns:tools="http://schemas.android.com/tools"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   tools:context="com.example.administrator.mycalculation.MainActivity">

 

    <LinearLayout

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:orientation="vertical">

        <TextView

           android:layout_width="wrap_content"

            android:layout_height="wrap_content"

           android:text="@string/title"

           android:layout_gravity="center"

            android:gravity="center"

            android:textSize="40sp"/>

 

        <TextViewandroid:id="@+id/result"

            android:layout_width="match_parent"

           android:layout_height="100dp"

           android:layout_gravity="center"

            android:gravity="bottom"

           android:text="@string/bcl"

            android:textSize="40sp"

           android:textAlignment="viewEnd"/>

 

        <TableLayout

           android:layout_width="fill_parent"

           android:layout_height="fill_parent"

           android:stretchColumns="0">

            <TableRow

               android:layout_height="wrap_content"

                android:layout_width="fill_parent">

                <Buttonandroid:id="@+id/bc"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/bc"

                    android:textSize="25sp"/>

                <Buttonandroid:id="@+id/bce"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/bce"

                    android:textSize="25sp"/>

                <Buttonandroid:id="@+id/bas"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/bas"

                    android:textSize="25sp"/>

                <Buttonandroid:id="@+id/bsq"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/bsq"

                    android:textSize="25sp"/>

            </TableRow>

            <TableRow

               android:layout_height="wrap_content"

               android:layout_width="fill_parent">

                <Buttonandroid:id="@+id/b1"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/b1"

                   android:textSize="25sp"/>

                <Buttonandroid:id="@+id/b2"

                    android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/b2"

                   android:textSize="25sp"/>

                <Buttonandroid:id="@+id/b3"

                    android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/b3"

                   android:textSize="25sp"/>

                <Buttonandroid:id="@+id/bad"

                    android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/bad"

                   android:textSize="25sp"/>

            </TableRow>

            <TableRow

                android:layout_height="wrap_content"

               android:layout_width="fill_parent">

                <Buttonandroid:id="@+id/b4"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/b4"

                   android:textSize="25sp"/>

                <Buttonandroid:id="@+id/b5"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                    android:text="@string/b5"

                   android:textSize="25sp"/>

                <Buttonandroid:id="@+id/b6"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                    android:text="@string/b6"

                   android:textSize="25sp"/>

                <Buttonandroid:id="@+id/bsu"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                    android:text="@string/bsu"

                   android:textSize="25sp"/>

            </TableRow>

            <TableRow

               android:layout_height="wrap_content"

               android:layout_width="fill_parent">

                <Button android:id="@+id/b7"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/b7"

                   android:textSize="25sp"/>

                <Buttonandroid:id="@+id/b8"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/b8"

                   android:textSize="25sp"/>

                <Buttonandroid:id="@+id/b9"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/b9"

                   android:textSize="25sp"/>

                <Button android:id="@+id/bmu"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/bmu"

                   android:textSize="25sp"/>

            </TableRow>

            <TableRow

               android:layout_height="wrap_content"

               android:layout_width="fill_parent">

                <Buttonandroid:id="@+id/bpo"

                   android:layout_height="wrap_content"

                    android:layout_width="wrap_content"

                   android:text="@string/bpo"

                   android:textSize="25sp"/>

                <Buttonandroid:id="@+id/b0"

                   android:layout_height="wrap_content"

                    android:layout_width="wrap_content"

                   android:text="@string/b0"

                   android:textSize="25sp"/>

                <Buttonandroid:id="@+id/beq"

                   android:layout_height="wrap_content"

                   android:layout_width="wrap_content"

                   android:text="@string/beq"

                   android:textSize="25sp"/>

                <Buttonandroid:id="@+id/bdi"

                   android:layout_height="wrap_content"

                    android:layout_width="wrap_content"

                   android:text="@string/bdi"

                   android:textSize="25sp"/>

            </TableRow>

        </TableLayout>

    </LinearLayout>

 

</android.support.constraint.ConstraintLayout>

 

 

 

String.xml

<resources>

    <stringname="app_name">CALCULATION</string>

    <stringname="bcl">0</string>

    <string name="title">计算器</string>

    <stringname="b0">0</string>

    <stringname="b1">1</string>

    <stringname="b2">2</string>

    <stringname="b3">3</string>

    <stringname="b4">4</string>

    <stringname="b5">5</string>

    <stringname="b6">6</string>

    <stringname="b7">7</string>

    <stringname="b8">8</string>

    <stringname="b9">9</string>

    <stringname="bad">+</string>

    <stringname="bsu">-</string>

    <stringname="bmu">*</string>

    <stringname="bdi">/</string>

    <stringname="bsq">√</string>

    <stringname="beq">=</string>

    <stringname="bpo">.</string>

    <stringname="bas">±</string>

    <stringname="bce">ce</string>

    <stringname="bc">c</string>

</resources>

 

Shiyan5jishiqi:

Main.java

packagecom.example.administrator.myapplication5;

 

importandroid.support.v7.app.AppCompatActivity;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.os.Message;

importandroid.app.Activity;

importandroid.view.Menu;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.TextView;

 

 

publicclass MainActivity extends AppCompatActivity {

 

    private TextView mint;

   private TextView sec;

   private Button start;

   private Button reset;

   private long timeusedinsec;

   private boolean isstop = false;

   private Handler mHandler = new Handler() {

       /*

        * edit by yuanjingchao 2014-08-04 19:10

         */

       @Override

     public void handleMessage(Message msg) {

                      // TODO Auto-generatedmethod stub

                      super.handleMessage(msg);

                       switch (msg.what) {

                           case 1:

                                    // 添加更新ui的代码

                                   if (!isstop) {

                                         updateView();

                                            mHandler.sendEmptyMessageDelayed(1,1000);

                                   }

                                  break;

                         case 0:

                             break;

                       }

       }

 

            };

 

 

    @Override

    protected void onCreate(BundlesavedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

        initViews();

 

    }

    private void initViews() {

               mint = (TextView)findViewById(R.id.mint);

               sec = (TextView)findViewById(R.id.sec);

              reset = (Button)findViewById(R.id.reset);

            start = (Button)findViewById(R.id.start);

            reset.setOnClickListener(new OnClickListener() {

          @Override

           public void onClick(Viewarg0) {

                              //TODO Auto-generated method stub

 

                             mint.setText("00");

                              sec.setText("00");

                            start.setText("start");

                            timeusedinsec=0;

                              isstop=true;

                         }

       });

             start.setOnClickListener(new OnClickListener() {

           @Override

          public void onClick(Viewarg0) {

                              //TODO Auto-generated method stub

                          mHandler.removeMessages(1);

                           Stringaaa=start.getText().toString();

                             if(aaa.equals("start")){

                                  mHandler.sendEmptyMessage(1);

                                      isstop = false;

                                     start.setText("停止");

                }else {

                                     mHandler.sendEmptyMessage(0);

                                      isstop = true;

                    start.setText("start");

                                 }

 

                           }

      });

          }

   private void updateView() {

               timeusedinsec += 1;

               int minute = (int)(timeusedinsec / 60)%60;

             int second = (int)(timeusedinsec % 60);

               if (minute < 10)

           mint.setText("0" + minute);

              else

                 mint.setText("" + minute);

               if (second < 10)

           sec.setText("0" + second);

               else

                 sec.setText("" + second);

          }

}

Main.xml

<?xmlversion="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:app="http://schemas.android.com/apk/res-auto"

   xmlns:tools="http://schemas.android.com/tools"

   android:layout_width="match_parent"

    android:layout_height="match_parent"

   tools:context="com.example.administrator.myapplication5.MainActivity">

    <RelativeLayout

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:layout_centerInParent="true" >

 

            <LinearLayout

                android:id="@+id/top"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:layout_centerHorizontal="true"

               android:orientation="horizontal" >

 

                <TextView

                   android:id="@+id/mint"

                   android:layout_width="wrap_content"

                   android:layout_height="wrap_content"

                    android:text="00"

                   android:textSize="30dp" />

 

                <TextView

                   android:layout_width="wrap_content"

                   android:layout_height="wrap_content"

                    android:text=":"

                    android:textSize="30dp" />

 

               <TextView

                  android:id="@+id/sec"

                  android:layout_width="wrap_content"

                  android:layout_height="wrap_content"

                    android:text="00"

                    android:textSize="30dp"/>

           </LinearLayout>

 

            <LinearLayout

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:layout_below="@+id/top"

              android:layout_centerHorizontal="true"

               android:orientation="horizontal" >

 

                <Button

                android:id="@+id/start"

                 android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                 android:text="计时"/>

 

               <Button

                android:id="@+id/reset"

                   android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                  android:text="清零" />

          </LinearLayout>

      </RelativeLayout>

 

    />

 

 

   </android.support.constraint.ConstraintLayout>

 

String:

<resources>

    <string name="app_name">计时器</string>

    <stringname="start">计时</string>

    <stringname="reset">清零</string>

 

</resources>

 

 

 

:

 


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页