android 계산기 단순 구현 코드

17976 단어 android계산기
본 논문 의 사례 는 안 드 로 이 드 계산기 의 구체 적 인 실현 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.


자바 코드:

package com.itheima74.simplecalculator4;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.cloud.util.ResourceUtil;

import java.util.ArrayList;

/**
 *      (   )
 *     :
 * 1.      
 * 2.      
 * 3.      (  )    SDK    :
 * a.  SDK,  APPID
 * b.  libs   Msc.jar   libs  ,  :add as library
 * c.  assets  ,  tts    3        
 * d.  jniLibs  ,  armeabi   ,x86  (         )
 * e.   SDK
 * f.          
 * g.    (    )
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

 private TextView tv;//     
 private ArrayList<String> mList;//         
 private double mResult;//     ,    0 ,      ,      0
 private boolean mFlag = true;//                ,    

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

 initSDK();
 initView();
 initList();
 }

 /**
 * 2.       
 *
 * @param flag            
 * @param msg       
 */
 private void initLocalHeCheng(boolean flag, String msg) {
 if (flag) {
  //1.  SpeechSynthesizer  ,     :     InitListener
  SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(this, new InitListener() {
  @Override
  public void onInit(int code) {

  }
  });
  //2.      
  //         
  mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);
  //       
  mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
  //        ,resPath         
  mTts.setParameter(ResourceUtil.TTS_RES_PATH, getResourcePath());
  //     
  mTts.setParameter(SpeechConstant.SPEED, "60");
  //     
  mTts.setParameter(SpeechConstant.PITCH, "50");
  //     
  mTts.setParameter(SpeechConstant.VOLUME, "100");
  //           
  mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");
  //          (        ),   “./sdcard/iflytek.pcm”
  //   SD    AndroidManifest.xml   SD   
  //           ,      
  //mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, "./sdcard/iflytek.pcm");
  //3.    
  mTts.startSpeaking(msg, mSynListener);
 }
 }

 //          
 private String getResourcePath() {
 StringBuffer tempBuffer = new StringBuffer();
 //       
 tempBuffer.append(ResourceUtil.generateResourcePath(this, ResourceUtil.RESOURCE_TYPE.assets, "tts/common.jet"));
 tempBuffer.append(";");
 //      
 tempBuffer.append(ResourceUtil.generateResourcePath(this, ResourceUtil.RESOURCE_TYPE.assets, "tts/xiaoyan.jet"));
 return tempBuffer.toString();
 }

 /**
 * 1.   SDK
 */
 private void initSDK() {
 SpeechUtility.createUtility(this, SpeechConstant.APPID + "=58ba4628");
 }

 //         
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 getMenuInflater().inflate(R.menu.main, menu);
 return true;
 }

 //            
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) {
  case R.id.exit:
  this.finish();
  break;
  case R.id.open_local_voice:
  mFlag = true;
  break;
  case R.id.close_local_voice:
  mFlag = false;
  break;
 }
 return super.onOptionsItemSelected(item);
 }


 /**
 * 2.      ,  
 *
 * @param flag        ,true:  ,false:   
 * @param msg        
 */
 /*private void initYuYinHeCheng(boolean flag, String msg) {
 if (flag) {
  //1.  SpeechSynthesizer  ,      :      InitListener
  SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(this, null);
  //2.      ,  《    MSC API  (Android)》SpeechSynthesizer  
  mTts.setParameter(SpeechConstant.VOICE_NAME, mVoiceName);//     
  mTts.setParameter(SpeechConstant.SPEED, "50");//    
  mTts.setParameter(SpeechConstant.VOLUME, "80");//    ,  0~100
  mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //    
  //          (        ),   “./sdcard/iflytek.pcm”
  //   SD    AndroidManifest.xml   SD   
  //           ,      
  //mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, "./sdcard/iflytek.pcm");
  //3.    
  mTts.startSpeaking(msg, mSynListener);
 }

 }*/

 //      
 private SynthesizerListener mSynListener = new SynthesizerListener() {
 //    
 @Override
 public void onSpeakBegin() {

 }

 //      
 //percent     0~100,beginPos             ,endPos              ,
 //info     。
 @Override
 public void onBufferProgress(int i, int i1, int i2, String s) {

 }

 //     
 @Override
 public void onSpeakPaused() {

 }

 //        
 @Override
 public void onSpeakResumed() {

 }

 //      
 //percent     0~100,beginPos             ,endPos              .
 @Override
 public void onSpeakProgress(int i, int i1, int i2) {

 }

 //        ,     ,error null
 @Override
 public void onCompleted(SpeechError speechError) {

 }

 //        
 @Override
 public void onEvent(int i, int i1, int i2, Bundle bundle) {

 }
 };


 /**
 *         3      
 */
 private void calculate() {
 double number1 = Double.parseDouble(mList.get(0));
 if (mList.size() == 3) {
  double number2 = Double.parseDouble(mList.get(2));
  switch (mList.get(1)) {
  case "+":
   mResult = number1 + number2;
   break;
  case "-":
   mResult = number1 - number2;
   break;
  case "*":
   mResult = number1 * number2;
   break;
  case "/":
   if (number2 == 0) {
   Toast.makeText(this, "     0", Toast.LENGTH_SHORT).show();
   } else {
   mResult = number1 / number2;
   }
   break;
  }
  //     
  mList.clear();
  mList.add(String.valueOf(mResult));
 } else {//   5=0   ,5+=   
  mResult = number1;
 }
 }

 /**
 *      
 */
 private void initList() {
 mList = new ArrayList<>();
 }

 /**
 *      ,      
 */
 private void initView() {
 tv = (TextView) findViewById(R.id.tv);
 findViewById(R.id.bt_0).setOnClickListener(this);
 findViewById(R.id.bt_1).setOnClickListener(this);
 findViewById(R.id.bt_2).setOnClickListener(this);
 findViewById(R.id.bt_3).setOnClickListener(this);
 findViewById(R.id.bt_4).setOnClickListener(this);
 findViewById(R.id.bt_5).setOnClickListener(this);
 findViewById(R.id.bt_6).setOnClickListener(this);
 findViewById(R.id.bt_7).setOnClickListener(this);
 findViewById(R.id.bt_8).setOnClickListener(this);
 findViewById(R.id.bt_9).setOnClickListener(this);
 findViewById(R.id.bt_add).setOnClickListener(this);
 findViewById(R.id.bt_sub).setOnClickListener(this);
 findViewById(R.id.bt_mul).setOnClickListener(this);
 findViewById(R.id.bt_div).setOnClickListener(this);
 findViewById(R.id.bt_dot).setOnClickListener(this);
 findViewById(R.id.bt_equal).setOnClickListener(this);
 findViewById(R.id.bt_clear).setOnClickListener(this);
 }

 //          
 @Override
 public void onClick(View view) {
 //        ,        
 String number = tv.getText().toString();

 switch (view.getId()) {
  case R.id.bt_0:
  if (!number.equals("0")) {//     0,    0
   tv.append("0");
  }
  initLocalHeCheng(mFlag, tv.getText().toString());
  break;
  case R.id.bt_1:
  if (number.equals("0")) {
   tv.setText("1");
  } else {
   tv.append("1");
  }
  initLocalHeCheng(mFlag, tv.getText().toString());
  break;
  case R.id.bt_2:
  if (number.equals("0")) {
   tv.setText("2");
  } else {
   tv.append("2");
  }
  initLocalHeCheng(mFlag, tv.getText().toString());
  break;
  case R.id.bt_3:
  if (number.equals("0")) {
   tv.setText("3");
  } else {
   tv.append("3");
  }
  initLocalHeCheng(mFlag, tv.getText().toString());
  break;
  case R.id.bt_4:
  if (number.equals("0")) {
   tv.setText("4");
  } else {
   tv.append("4");
  }
  initLocalHeCheng(mFlag, tv.getText().toString());
  break;
  case R.id.bt_5:
  if (number.equals("0")) {
   tv.setText("5");
  } else {
   tv.append("5");
  }
  initLocalHeCheng(mFlag, tv.getText().toString());
  break;
  case R.id.bt_6:
  if (number.equals("0")) {
   tv.setText("6");
  } else {
   tv.append("6");
  }
  initLocalHeCheng(mFlag, tv.getText().toString());
  break;
  case R.id.bt_7:
  if (number.equals("0")) {
   tv.setText("7");
  } else {
   tv.append("7");
  }
  initLocalHeCheng(mFlag, tv.getText().toString());
  break;
  case R.id.bt_8:
  if (number.equals("0")) {
   tv.setText("8");
  } else {
   tv.append("8");
  }
  initLocalHeCheng(mFlag, tv.getText().toString());
  break;
  case R.id.bt_9:
  if (number.equals("0")) {
   tv.setText("9");
  } else {
   tv.append("9");
  }
  initLocalHeCheng(mFlag, tv.getText().toString());
  break;
  case R.id.bt_add:
  //     
  initLocalHeCheng(mFlag, " ");
  // 1.            mList
  mList.add(number);
  // 2.      3    
  this.calculate();
  // 3. "+"   mList
  mList.add("+");
  // 4.tv 0
  tv.setText("0");
  break;
  case R.id.bt_sub:
  //     
  initLocalHeCheng(mFlag, " ");
  // 1.            mList
  mList.add(number);
  // 2.      3    
  this.calculate();
  // 3. "-"   mList
  mList.add("-");
  // 4.tv 0
  tv.setText("0");
  break;
  case R.id.bt_mul:
  //     
  initLocalHeCheng(mFlag, " ");
  // 1.            mList
  mList.add(number);
  // 2.      3    
  this.calculate();
  // 3. "*"   mList
  mList.add("*");
  // 4.tv 0
  tv.setText("0");
  break;
  case R.id.bt_div:
  //     
  initLocalHeCheng(mFlag, " ");
  // 1.            mList
  mList.add(number);
  // 2.      3    
  this.calculate();
  // 3. "/"   mList
  mList.add("/");
  // 4.tv 0
  tv.setText("0");
  break;
  case R.id.bt_dot:
  // 1..53       ?xml    0,clear    0,        
  // 2.0.53.5,  ,      .,      .
  if (!number.contains(".")) {//      .   
   tv.append(".");
  }
  break;
  case R.id.bt_equal:
  // 1.            mList
  mList.add(number);
  // 2.      3    
  this.calculate();
  // 3.tv    
  tv.setText(String.valueOf(mResult));
  // 4.    
  initLocalHeCheng(mFlag, "  " + tv.getText().toString());
  // 5.    
  mList.clear();
  break;
  case R.id.bt_clear:
  //     
  initLocalHeCheng(mFlag, "  ");

  tv.setText("0");
  mResult = 0;
  mList.clear();
  break;
 }
 }

}
xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity">

 <TextView
 android:id="@+id/tv"
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="2"
 android:background="#000"
 android:gravity="center_vertical|end"
 android:maxLines="1"
 android:text="@string/text"
 android:textColor="#fff"
 android:textSize="50sp" />

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="1"
 android:orientation="horizontal">

 <Button
  android:id="@+id/bt_7"
  style="@style/Button_Style"
  android:text="@string/_7" />

 <Button
  android:id="@+id/bt_8"
  style="@style/Button_Style"
  android:text="@string/_8" />

 <Button
  android:id="@+id/bt_9"
  style="@style/Button_Style"
  android:text="@string/_9" />

 <Button
  android:id="@+id/bt_div"
  style="@style/Button_Style"
  android:text="@string/div" />
 </LinearLayout>

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="1"
 android:orientation="horizontal">

 <Button
  android:id="@+id/bt_4"
  style="@style/Button_Style"
  android:text="@string/_4" />

 <Button
  android:id="@+id/bt_5"
  style="@style/Button_Style"
  android:text="@string/_5" />

 <Button
  android:id="@+id/bt_6"
  style="@style/Button_Style"
  android:text="@string/_6" />

 <Button
  android:id="@+id/bt_mul"
  style="@style/Button_Style"
  android:text="@string/mul" />
 </LinearLayout>

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="1"
 android:orientation="horizontal">

 <Button
  android:id="@+id/bt_1"
  style="@style/Button_Style"
  android:text="@string/_1" />

 <Button
  android:id="@+id/bt_2"
  style="@style/Button_Style"
  android:text="@string/_2" />

 <Button
  android:id="@+id/bt_3"
  style="@style/Button_Style"
  android:text="@string/_3" />

 <Button
  android:id="@+id/bt_sub"
  style="@style/Button_Style"
  android:text="@string/sub" />
 </LinearLayout>

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="1"
 android:orientation="horizontal">

 <Button
  android:id="@+id/bt_0"
  style="@style/Button_Style"
  android:text="@string/_0" />

 <Button
  android:id="@+id/bt_dot"
  style="@style/Button_Style"
  android:text="@string/dot" />

 <Button
  android:id="@+id/bt_equal"
  style="@style/Button_Style"
  android:text="@string/equal" />

 <Button
  android:id="@+id/bt_add"
  style="@style/Button_Style"
  android:text="@string/add" />
 </LinearLayout>

 <Button
 android:id="@+id/bt_clear"
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="1"
 android:text="@string/clear"
 android:textAllCaps="false"
 android:textSize="25sp" />

</LinearLayout>
계산기 에 관 한 멋 진 글 은 《계산기 특집》. 을 보 세 요.더 많은 멋 진 것 을 발견 하 기 를 기다 리 겠 습 니 다!
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기