2014-03-19 46 views
-2

我必須編輯文本,如果我點擊添加按鈕而不輸入任何東西到edittext我得到fc。如果用戶沒有輸入所需信息,我該如何顯示提示

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.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;; 

    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(); 

    } 

    public void doSomething(View v) { 
     AlertDialog.Builder a1 = new AlertDialog.Builder(c); 

     if (v.getId() == R.id.add) { 
      if (a==null & b==null) { 
Log.e("hash","asdasd"); 
       // 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 { 

      int result = Integer.parseInt(a) + Integer.parseInt(b); 
      t1.setText(Integer.toString(result)); 
     } 
    } 

} 
+3

請不要喊叫*(我已經在這個場合爲你糾正它。)* –

+0

澄清你的問題。 –

回答

0

請刪除 「公共無效DoSomething的(視圖V)」 的是INSEAD,

使用按鈕onclicklistener像低於活動的OnCreate中(),

add = (Button) findViewById(R.id.add); 
    add .setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
if(e1.getText().toString().equals("") || e2.getText().toString().equals("")) 
{ 
      new AlertDialog.Builder(MainActivity.this) 
      .setTitle("INPUT ALERT") 
      .setIcon(R.drawable.galleryalart) 
      .setMessage("PLEASE ENTER THE INPUT??") 

      .setPositiveButton("OK", new OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 


      } 
     }).create().show(); 

     } 
    else { 

     int result = Integer.parseInt(e2.getText().toString()) + Integer.parseInt(e2.getText().toString()); 
     t1.setText(result+""); 
    } 

     } 
    }); 
相關問題