【问题标题】:TextUtils how to implement?TextUtils如何实现?
【发布时间】:2015-06-09 19:28:30
【问题描述】:

我不确定如何在我的代码中正确实现 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);
  }
}    

【问题讨论】:

  • 什么是按钮?在第一个代码块中,您将实例化它的两个版本,一个数组和一个浮点数。您的代码有点混乱,因为您一遍又一遍地使用相同的名称(按钮),很难理解应该发生什么。
  • 您是在检查TextView scr 还是Button 按钮上的空白?你为什么不直接去Whateverview.getText().toString().isEmpty()Whateverview.getText().toString().length() == 0
  • 嘿@Elltz 我正在检查 Button 按钮是否为空。我尝试了“Whateverview.getText().toString().isEmpty() 或 Whateverview.getText().toString().length() == 0 ”然后说无法解析符号“getText”我只想要所有按钮在数组中具有功能。
  • 如果它的按钮在你的按钮对象上if(button.getText().toString().length() == 0){ // its empty};
  • 您应该让人们知道您在这个问题上的状态吗,先生?

标签: java android arrays


【解决方案1】:

对于数组中的按钮,您的空检查应该同时以这种方式进行

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

【讨论】:

    【解决方案2】:

    如果要检查 Button 是否为空,那么

    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 我猜这是你的问题

    【讨论】:

      猜你喜欢
      • 2014-09-13
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-24
      • 2014-09-06
      • 2013-12-16
      • 2011-02-17
      相关资源
      最近更新 更多