2013-09-21 70 views
-3
package com.example.app123; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    EditText input; 
    EditText output; 
    Button one; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     input = (EditText) findViewById(R.id.editText1); 
     output = (EditText) findViewById(R.id.editText2); 
     one = (Button) findViewById(R.id.button1); 
     one.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (v == one) { 
        Context context1 = getApplicationContext(); 
        CharSequence text1 = "Please enter a valid number"; 
        int duration = Toast.LENGTH_SHORT; 
        final Toast toast = Toast.makeText(context1, text1, 
          duration); 
        toast.show(); 
        int inputValue = Integer.parseInt(input.getText() 
          .toString()); 
        int value = inputValue * inputValue; 
        output.setText(value); 
       } 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it ispresent. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 
+0

在這行你得到錯誤? –

+0

發佈logcat –

+0

爲什麼你再次使用'v == one'代碼? –

回答

0

喜消除這種狀況在您按一下按鈕的代碼,如果(V ==一個)只能用這個像

 input=(EditText)findViewById(R.id.editText1); 
    output = (EditText) findViewById(R.id.editText2); 
    one = (Button) findViewById(R.id.button1); 
    one.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

       Context context1 = getApplicationContext(); 
       CharSequence text1 = "Please enter a valid number"; 
       int duration = Toast.LENGTH_SHORT; 
       final Toast toast = Toast.makeText(context1, text1, 
         duration); 
       toast.show(); 
       int inputValue = Integer.parseInt(input.getText() 
         .toString()); 
       int value = inputValue * inputValue; 
       output.setText(value); 

     } 
    }); 

我希望這是你的工作。

+0

你確定** if(v == one)**創建錯誤嗎? –

+0

你是直接使用按鈕點擊事件在android中使用btnobj.setonclicklistener,所以你爲什麼檢查點擊查看是按鈕對象?它的工作或不請回復。 –

+0

是的..沒有需要的條件,但如果有條件我不這樣認爲,它會拋出錯誤.... –

0

如果你有多個按鈕:

Button b1=(Button)findviewById(R.id.b1); 
Button b2=(Button)findviewById(R.id.b2); 
one.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      switch(v.getid()) 
      case R.id.b1: 
       CharSequence text1 = "Please enter a valid number"; 
       int duration = Toast.LENGTH_SHORT; 
       final Toast toast = Toast.makeText(YourActivity.this, text1, 
         duration); 
       toast.show(); 
       int inputValue = Integer.parseInt(input.getText() 
         .toString()); 
       int value = inputValue * inputValue; 
       output.setText(value); 
      break; 
      case R.id.b1: 
      break; 
     } 
    });