2015-04-05 83 views
1

我不知道如何在我的代碼中正確實現TextUtils。我正在構建一個計算器,但不知道應該如何實施?這可能是一個小小的修復,但不能確定。提前致謝!TextUtils如何實現?

繼承人我的代碼:

package com.mula.minicalculator; 
import android.text.TextUtils; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Button; 

import static com.mula.minicalculator.R.id; 


public class MainActivity extends ActionBarActivity implements View.OnClickListener { 

TextView scr; 
Button buttonPlus; 
Button buttonSub; 
Button buttonX; 
Button buttonDiv; 

String oper = ""; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    scr = (TextView) findViewById(id.answerSum); 

    // Elements 
    buttonDiv = (Button) findViewById(id.buttonDiv); 
    buttonX = (Button) findViewById(id.buttonX); 
    buttonSub = (Button) findViewById(id.buttonSub); 
    buttonPlus = (Button) findViewById(id.buttonPlus); 



    // Set a Listener 
    buttonDiv.setOnClickListener(this); 
    buttonSub.setOnClickListener(this); 
    buttonX.setOnClickListener(this); 
    buttonPlus.setOnClickListener(this); 

    // Now the numbers 

    Button buttons[] = new Button[10]; 

    buttons[0] = (Button) findViewById(id.button0); 
    buttons[1] = (Button) findViewById(id.button1); 
    buttons[2] = (Button) findViewById(id.button2); 
    buttons[3] = (Button) findViewById(id.button3); 
    buttons[4] = (Button) findViewById(id.button4); 
    buttons[5] = (Button) findViewById(id.button5); 
    buttons[6] = (Button) findViewById(id.button6); 
    buttons[7] = (Button) findViewById(id.button7); 
    buttons[8] = (Button) findViewById(id.button8); 
    buttons[9] = (Button) findViewById(id.button9); 


} 


@Override 
public void onClick(View view) { 

    float buttons = 0; 
    float result = 0; 

//問題就在這裏

if (TextUtils.isEmpty(buttons.getText().toString()) { 
     return; 
    } 

    return; 


    switch (view.getId()) { 
     case id.buttonPlus: 
      oper = "+"; 
      result = buttons + buttons; 
      break; 
     case id.buttonSub: 
      oper = "-"; 
      result = buttons - buttons; 
      break; 
     case id.buttonX: 
      oper = "*"; 
      result = buttons * buttons; 
      break; 
     case id.buttonDiv: 
      oper = "/"; 
      result = buttons/buttons; 
      break; 
     default: 
      break; 


    } 

    scr.setText(buttons + " " + oper + " " + buttons + " = " + result); 
    } 
}  
+0

是什麼按鈕?在第一個代碼塊中,你正在實例化它的兩個版本,一個數組和一個浮點數。你的代碼有點混亂,因爲你反覆使用同一個名字(按鈕),很難理解應該發生什麼。 – Zamereon 2015-04-05 03:01:37

+0

你在檢查'TextView' scr或'Button'按鈕上的空白嗎?爲什麼你不只是去'Whateverview.getText()。的toString()。的isEmpty()'或'Whateverview.getText()。的toString()。長度()== 0' – Elltz 2015-04-05 03:01:45

+0

嘿@Elltz我檢查按鈕按鈕的空白。我嘗試了「Whateverview.getText()。toString()。isEmpty()或Whateverview.getText()。toString()。length()== 0」然後說不能解析符號「getText」我只想要所有的按鈕在數組中具有功能。 – 2015-04-05 03:53:28

回答

0

你的空檢查應同時在這樣的按鈕排列

if (TextUtils.isEmpty(buttons[0].getText().toString()) 
{ 
return; 
} 
0

,如果你想選擇按鈕的空虛,然後

button.getText().toString().length() == 0 //all versions 

也是在你的代碼檢查該

if (TextUtils.isEmpty(buttons.getText().toString()) { 
    return; // this takes you out of the method, if its empty 
} 

return; // this takes you out of the method,even if its not, 
//switch statements wont be called 

所以刪除第二個return我想這是你的問題