2014-03-19 2 views
0

edittext가 비어 있는지 확인하는 if 조건이 있습니다. 비어 있으면 경고가 발생합니다. 나는 글고 몇 가지 값을 입력 할 때 다음 또한 그것은 비어있는 경우 경고가 심해질 때이다, 나에게 나는 글고 치기가 비어인지 아닌지 확인하는 경우 조건이다른 조건이 호출되지 않음

package com.example.calculator; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Calci extends Activity { 
    TextView t1; 
    EditText e1, e2; 
    Button add, sub, mul, div; 
    Context c=this; 

    String b, a; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_calci); 
     e1 = (EditText) findViewById(R.id.EditText01); 
     e2 = (EditText) findViewById(R.id.EditText02); 
     add = (Button) findViewById(R.id.add); 
     sub = (Button) findViewById(R.id.sub); 
     mul = (Button) findViewById(R.id.mul); 
     div = (Button) findViewById(R.id.div); 
     t1 = (TextView) findViewById(R.id.textView1); 
     a = e1.getText().toString(); 
     b = e2.getText().toString(); 
add.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     if (a.equals("") || b.equals("")){ 

      AlertDialog.Builder a1 = new AlertDialog.Builder(c); 
       // Setting Dialog Title 
       a1.setTitle("Alert Dialog"); 
       // Setting Dialog Message 
       a1.setMessage("PLEASE ENTER SOMETHING"); 
       a1.setPositiveButton("yes", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int button1) { 
           // if this button is clicked, close 
           // current activity 
           dialog.cancel(); 
          } 
          }); 

       // Showing Alert Message 
       AlertDialog alertDialog = a1.create(); 
       a1.show(); 

      } else { 
      Log.d("sadasd", "msg"); 
      int result = Integer.parseInt(a) + Integer.parseInt(b); 
      t1.setText(Integer.toString(result)); 
//   InputMethodManager imm = (InputMethodManager)getSystemService(
//     Context.INPUT_METHOD_SERVICE); 
//    imm.hideSoftInputFromWindow(add.getWindowToken(), 0); 
     } 

    } 

}); 
    } 
} 

경고를주고있다 edittext에 몇 가지 값을 입력 한 다음 알림을 보내고 있습니다.

+0

e1과 e2 모두에 무언가를 입력해야합니다. – Blackbelt

+1

중단 점을 추가하고 코드를 디버깅하십시오. –

답변

1

onCreate() 방법으로 잘못하고 있습니다. 당신은

@Override 
public void onClick(View arg0) 
{ 
    a = e1.getText().toString(); 
    b = e2.getText().toString(); 
    if (a.equals("") || b.equals("")) 
    { 

실제 이유는 당신이 그냥 initilize대로에서 OnCreate() 메서드 글고 치기가 비어 있음, 아래처럼 onClick() 방법을 수행해야합니다. 필요한 것은 텍스트 init을 삽입하고 조건을 확인하는 것입니다. 따라서 당신은

+0

downvote의 이유는 무엇입니까? – Kedarnath

+1

그가 무엇을해야하는지 설명하는 것이 좋을 것입니다/안쪽에서하지 마십시오 onCreate – Blackbelt

+0

@blackbelt 죄송합니다. 나는 대답을 쓰고있었습니다. – Kedarnath

1

이 시도() 메소드 onClicck에서 a = e1.getText().toString(); & b = e2.getText().toString(); 문을 작성해야합니다 ..

ClickListener

add.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     a = e1.getText().toString(); 
     b = e2.getText().toString(); 
     if (a.equals("") || b.equals("")){ 
     . 
     . 
     . 
} 
0

내부의 EditText 텍스트를 받기는 클릭 리스너에 안에 있어야 업데이 트하는 버튼을 누르기 전의 값 :

a = e1.getText().toString(); 
b = e2.getText().toString(); 
0

조건 if (a.equals("") || b.equals(""))이 아니고ab 모두 내부에 값이있을 때만 지나갈 것입니다. 따라서 간단히 editTexts을 모두 입력해야하며, 그 순간에는 활동이 시작될 때만 간단하게 수행하기 때문에 onClick listener 상단에서도 확인해야합니다. 따라서 변수의 값은 a and b입니다.

add.setOnClickListener(new View.OnClickListener() { 
    a = e1.getText().toString(); 
    b = e2.getText().toString(); 
관련 문제