2016-12-20 4 views
0

は誰かが私はbuttonを「追加」をクリックした後に私のアプリでどこかに表示することができますvariablefloatに追加EditTextからinputを取得CAG方法を教えてくださいことはできますか? これは、XMLの私EditTextです:EditText入力を変数floatに追加するにはどうすればよいですか?

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="numberDecimal" 
    android:ems="10" 
    android:id="@+id/depositInput" 
    android:hint="enter a sum" /> 

そして、これは、同じXMLの私Button次のとおりです。

<Button 
    android:text="add transaction 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/addDepositButton" /> 

そして、ここでは、これがclassで行わ得るために、私の試みです:

public class AddMoneyTransaction extends AppCompatActivity { 

Button addDepositButton; 
EditText depositInput; 
float inputValue; 

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

    addDepositButton = (Button)findViewById(R.id.addDepositButton); 
    depositInput = (EditText)findViewById(R.id.depositInput); 

    addDepositButton.setOnClickListener(
      new View.OnClickListener() 
      { 
       public void onClick(View view) 
       { 
        // get EditText by id 
        (EditText) inputValue = (EditText) findViewById(R.id.depositInput); 

        float actualBalance + inputValue 
       } 
      }); 
} 
} 

これは機能しないことがわかります。それが私が求めている理由です。 }括弧で書かれた小さな書式の間違いを無視してください(これは実際のクラスではなく、この投稿にのみあります)。 Thank you.

答えて

0

セミ擬似コード:

// get the view 
(EditText) inputView = (EditText) findViewById(R.id.depositInput); 

// get the string inside the EditText 
String inputStringValue = inputView.getText().toString(); 

// parse it to a long 
long inputLongValue = Long.valueOf(inputStringValue) 
0

これはあなたを助けることを願っていますが...

// get EditText by id 
       (EditText) inputValue = (EditText) findViewById(R.id.depositInput); 

       float actualBalance + inputValue 

をこれらの行を削除し、

float number = Float.valueOf(depositInput.getText().toString()); 

....これを試してみてください。

0

テキストコンテンツを取得してから、それを浮動小数点数に変換するのはどうですか?

CharSequence text = intputValue.getText(); 
StringBuilder sb = new StringBuilder(text); 
float f = Float.parseFloat(sb.toString()); 
1
float inputValueFloat = Float.parseFloat("" + inputValue.getText()); 

"" + inputValue.getText()String

Float.parseFloat(String)

に入力されたテキストを変換する

を浮上さ String値を変換するためのものです
関連する問題