【问题标题】:Android EditText Decimal Integer ValueAndroid EditText 十进制整数值
【发布时间】:2012-01-28 13:17:21
【问题描述】:

我有以下具有文本视图和编辑文本的应用程序。当edittext 的值改变时,它会更新一个textview 的内容。现在我希望将值作为整数接收。这是当前的应用程序:

package your.test.two;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class TesttwoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    EditText input = (EditText)findViewById(R.id.editText1);
    input.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s){
            update();
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    });
}

public void update(){
    EditText source = (EditText)findViewById(R.id.editText1);
    Editable sourceData = source.getText();
    TextView destination = (TextView)findViewById(R.id.textView1);
    destination.setText(sourceData);
}
}


现在我希望以某种方式将可编辑的 sourceData 转换为整数,以便我可以对其执行 if 语句,如下所示:

if(sourceData>255){
  sourceData = 0;
}

我已经在 main.xml 中更改了 edittext 的值,方法是将以下几行添加到 editText1:

android:inputType="number"
android:maxLength="3"
android:numeric="integer"

请给我一些指导方针,告诉我如何做到这一点。一直在谷歌上搜索,但我最终得到的是:

Integer i = Integer.parseInt(edittext.gettext().tostring());

&

Integer i = Integer.valueOf(String s);

而且我还没有完全让那些工作或理解它。请帮忙!? :(

【问题讨论】:

    标签: android string integer android-edittext


    【解决方案1】:

    你应该可以使用

    Integer i = Integer.parseInt(edittext.gettext().tostring());
    

    通过将 i 替换为您想要将整数设置为的变量,在您的情况下为“sourceData”,并将 edittext 替换为您的编辑文本框的名称,因此为“source”

    所以你会得到这样的东西:

    public void update(){
        EditText source = (EditText)findViewById(R.id.editText1);
        Integer sourceData = Integer.parseInt(source.getText().toString());
        TextView destination = (TextView)findViewById(R.id.textView1);
        destination.setText(Integer.toString(sourceData));
    }
    }
    

    【讨论】:

    • 啊,谢谢各位!那段代码正是我要找的! destination.setText(Integer.toString(sourceData));你真的帮了我。谢谢!
    【解决方案2】:

    尽管您可以将 EditText 的 XML 中的属性设置为数字,但这只会阻止用户输入其他非数字信息。 EditText 中的文本将始终是一个字符串。不过,您可以将其转换为整数,然后执行您的条件:

    public void update() {
        EditText source = (EditText)findViewById(R.id.editText1);
        int sourceData = Integer.valueOf(source.getText().toString());
    
        if (sourceData > 255) {
            sourceData = 0;
        }
    
        TextView destination = (TextView)findViewById(R.id.textView1);
        destination.setText(Integer.toString(sourceData));
    }
    

    【讨论】:

    • 啊,谢谢各位!那段代码正是我要找的! destination.setText(Integer.toString(sourceData));你真的帮了我。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 2021-09-29
    • 2014-04-16
    相关资源
    最近更新 更多