我有以下具有textview和edittext的應用程序。當edittext的值發生變化時,它會更新textview的內容。現在我想要將該值作爲整數接收。這是當前的應用程序:
Android EditText十進制整數值
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(sourceData>255){
sourceData = 0;
}
我已經通過將以下行添加到editText1中來更改main.xml中edittext的值:
android:inputType="number"
android:maxLength="3"
android:numeric="integer"
有人可以給我一些關於如何做到這一點的指導,請。被谷歌搜索,但所有我最終是喜歡的東西:
Integer i = Integer.parseInt(edittext.gettext().tostring());
&
Integer i = Integer.valueOf(String s);
而且我不太讓那些工作或理解它。請幫忙!? :(
Aaah thanx guys!這段代碼就是我一直在尋找的東西! destination.setText(Integer.toString(sourceData)); 你真的幫了我。感謝名單! – Anomaly 2011-12-27 14:36:13