首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TextUtils如何实现?

TextUtils如何实现?
EN

Stack Overflow用户
提问于 2015-04-05 02:57:28
回答 2查看 3.4K关注 0票数 1

我不知道如何在代码中正确地实现TextUtils。我正在构建一个计算器,但不确定它应该如何实现?这可能是个小问题,但不确定。提前感谢!

这是我的密码:

代码语言:javascript
复制
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;

//这里的问题

代码语言:javascript
复制
    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);
  }
}    
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-05 04:54:18

您的空检查应该以这种方式同时检查数组中的按钮。

代码语言:javascript
复制
if (TextUtils.isEmpty(buttons[0].getText().toString()) 
{ 
    return; 
} 
票数 0
EN

Stack Overflow用户

发布于 2015-04-05 04:27:05

如果你想检查巴顿的空虚

代码语言:javascript
复制
button.getText().toString().length() == 0 //all versions

同样在您的代码中,请检查以下内容

代码语言:javascript
复制
 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 --我猜这是你的问题

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29453667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档