2014-06-15 5 views
-1

나는 EditText에서 텍스트의 값을 정수로 얻으려고하지만, 모든 값을 삽입 할 때마다 catch에서 초기화 된 값을 계산하므로 다음은이 문제EditText에서 정수 변수로 텍스트를 가져 오는 방법

에 대한 해결 무슨 결과는 아마 당신의 문자열에 숯을 또는 이때 공백 숫자되지 않는 = 0

public Button bu,bu2,bu3,bu4,bu5; 
int num=0,num2=0,num3=0,result=0; 
public EditText on,on2,on3,on4; 
public TextView tex ; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_monthlycalc); 

    bu=(Button)findViewById(R.id.year); 
    bu.setOnClickListener(this); 

    bu2=(Button)findViewById(R.id.calc); 
    bu2.setOnClickListener(this); 

    bu3=(Button)findViewById(R.id.reset); 
    bu3.setOnClickListener(this); 

    bu4=(Button)findViewById(R.id.H1); 
    bu4.setOnClickListener(this); 

    bu5=(Button)findViewById(R.id.ron); 
    bu5.setOnClickListener(this); 


    try { 
     on=(EditText)findViewById(R.id.loanAm); 
     num=Integer.valueOf(on.getText().toString().trim()); 
    } catch (NumberFormatException e) { 
     num=0; 
    } 

    try { 
     on2=(EditText)findViewById(R.id.intRate); 
     num2=Integer.valueOf(on2.getText().toString().trim()); 
    } catch (NumberFormatException e) { 
     num2=0; 
    } 

    try { 
     on3=(EditText)findViewById(R.id.lonTerm); 
     num=Integer.valueOf(on3.getText().toString().trim()); 
    } catch (NumberFormatException e) { 
     num3=0; 
    } 
    tex = (TextView) findViewById(R.id.monPay); 
    result=num+num2+num3; 
@Override 
public void onClick(View v) 
{ 
    if (v.getId()==R.id.calc) 
     tex.setText(String.valueOf(result));  
} 

답변

0
당신은 활동이 시작되는 동안 발생 onCreate 내부의 값을 얻기 위해 노력하고있다 onClick 에 코드를 이동

모든 EditText의는 onClick 내부를 빈 넣어 그것은 작동합니다 :)

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_monthlycalc); 
    bu=(Button)findViewById(R.id.year); 
    bu.setOnClickListener(this); 
    bu2=(Button)findViewById(R.id.calc); 
    bu2.setOnClickListener(this); 
    bu3=(Button)findViewById(R.id.reset); 
    bu3.setOnClickListener(this); 
    bu4=(Button)findViewById(R.id.H1); 
    bu4.setOnClickListener(this); 
    bu5=(Button)findViewById(R.id.ron); 
    on=(EditText)findViewById(R.id.loanAm); 
    on2=(EditText)findViewById(R.id.intRate); 
    on3=(EditText)findViewById(R.id.lonTerm); 
    tex = (TextView) findViewById(R.id.monPay); 
} 


@Override 
public void onClick(View v) 
{ 
    try { 
     num=Integer.valueOf(on.getText().toString().trim()); 
    } catch (NumberFormatException e) { 
     num=0; 
    } 

    try { 
     num2=Integer.valueOf(on2.getText().toString().trim()); 
    } catch (NumberFormatException e) { 
     num2=0; 
    } 

    try { 
     num=Integer.valueOf(on3.getText().toString().trim()); 
    } catch (NumberFormatException e) { 
     num3=0; 
    } 
    result=num+num2+num3; 
    if (v.getId()==R.id.calc) 
     tex.setText(String.valueOf(result));  
} 
+0

나는 그것을했지만이 경우 첫 번째 숫자는 합계에 추가하지 않았다. @Shereef Marzouk – Haredy

+0

나는 이해하지 못한다. 더 설명 할 수 있니? –

+0

이 경우 결과는 num2 + num3 만됩니다. 그러나 아무 것도 변경하지 않고 onClick 메서드로 이동합니다. – Haredy

0

당신이 가지고 있기 때문에이된다.

정규식을 사용하여 구문 분석하십시오.

final Pattern pattern = Pattern.compile("\\d+"); // the regex 
final ArrayList<Integer> ints = new ArrayList<Integer>(); // results 
while (matcher.find()) { // for each match 
    ints.add(Integer.parseInt(matcher.group())); // convert to int 
} 

정말로 모든 공백을 제거하려면 트림을 사용하지 마십시오. 사용

aswell 모든 탭을 제거하고 당신이 DEFAULTVALUE을 정의 해달라고하면 당신이하지 전에 버튼을 클릭하면이 그나마 초기화하는 동안 내부의 값이 이후 값, 잡고 있는지 확인

st.replaceAll("\\s+","") 

.

관련 문제