2016-06-22 12 views
-2

電卓を作成しています。たとえば、ボタンを押したときに5、ボタンに値5を設定する方法、ボタンをクリックするとします。値は編集テキストで印刷する必要があります!EditTextとボタン(ボタンを押すと、edittextフィールドに値が入力されます)

私のソースコード:

ed=(EditText)findViewById(R.id.editText); 

b=(Button)findViewById(R.id.button); 

gt=Double.parseDouble(ed.getText().toString()); 

ed.setText(""+gt); 

} 
+0

完全なソースコード 公共ボイド追加(ビュービュー) {ディスプレイ(); } プライベートvoid display() { EditText ed; >ボタンb; double gt; –

+1

質問を正しく編集してください。 – surajsn

答えて

1

もし私が正しくあなたの質問を理解する:

Uは、テキストまたは数字でいくつかのボタンを得ました。 誰かがボタンを押すと、editTextボックスにボタンのテキストが表示されますか?

次のようなので、uが何かを行うことができます場合は、次の

EditText myEditText = (EditText) findViewById(R.id.editText); 

Button myButton0= (Button) findViewById(R.id.button0); 
myButton0.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     //Get the button text and display it in the editText 
     //This will replace all text in the editText box 
     myEditText.setText(myButton0.getText().toString()); 

     //Or (Thanks to @cherry-wave) 
     //This will append the text, instead of replacing 
     myEditText.setText(myEditText.getText() + myButton0.getText().toString()); 
    } 
}); 

それとも(1つの方法だけですべてのボタンを処理するために):
は、あなたのレイアウトXMLにすべてのボタンに以下の行を追加します。 :

android:onClick="onClick" 

とは、あなたの活動のクラスファイルに次のメソッドを配置します。

//Your onClick method 
public void onClick(View v) { 
    Button myButton = (Button)v; 
    //Don't forget to declare your edittext 
    //This will replace all text in the editText box 
    myEditText.setText(myButton.getText().toString()); 

    //Or (Thanks to @cherry-wave) 
    //This will append the text, instead of replacing 
    myEditText.setText(myEditText.getText() + myButton.getText().toString()); 
} 

希望すると、これはうまくいきます。

+1

さらに、これが実際に電卓の場合:まず、編集テキストのテキストを取得し、テキストを設定し、クリックされたボタンの値を追加します。 –

+1

いいです!それをソリューションに追加しました – Strider

0
Take the value from button and add to edittext . 
    Just like this . 
    editText.setText(btnFive.getText().toString()); 
Before button click get value from editText . 
String edtValue = editText.getText.toString(); 
Than set editText.setText(edtValue+btnFive.getText().toString()); 
関連する問題