0

私はプログラミングにかなり慣れていないので、Androidデバイス用のアプリを作成する方法を独学しようとしていました. 現在、公式の Android 開発者 Web サイトの元のユーザー ガイドを使用しています。ユーザーから整数入力を取得しようとしていますが、ボタンをクリックすると、数値に .1 * (2 番目のユーザー編集テキスト ボックスから取得した数値) が乗算されます。たとえば、ユーザーは最初のテキスト ボックスに 100 を入力し、2 番目のテキスト ボックスに 10 を入力します。3 番目のテキスト ボックスの結果は 10/100*100 となり、これは 10 になります。結果が 3 番目のテキスト ボックスに表示されるようにします。

ここに私がこれまでに持っているコードがあります

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


public class FirstActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.discountpricecalculator.MESSAGE";
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void sendMessage(View view) {
    //Do something in response to button

}

}

4

2 に答える 2

3

を使用して edittext 値を取得します

EditText e1 = (EditText) findViewById(R.id.edittext1);
String myEditValue = e1.getText().toString();
int value = Integer.parseInt(myEditValue);  //similarly for second text view

必要な数学的操作を行います

value 3=value2*value1

設定値と同様

EditText e3 = (EditText) findViewById(R.id.edittext3);
e3.setText(Value3.toString());
于 2013-06-10T05:42:51.827 に答える
1

比較的完全な例を次に示します。ビュー XML を自分で理解する必要があります。

public class YourActivity extends Activity {

    private EditText first;
    private EditText second;
    private TextView result;

    private Button button;


    public void onCreate(Bundle instanceState) {
        super.onCreate(instanceState);
        first = (EditText)findViewById(R.id.idoffirst);
        second = (EditText)findViewById(R.id.idofsecond);
        result = (TextView)findViewById(R.id.idofresult);
        button = (Button)findViewById(R.id.idofbutton);

        // limit input to numbers only (or use android:inputType="number" in XML)
        first.setInputType(InputType.TYPE_CLASS_NUMBER);
        second.setInputType(InputType.TYPE_CLASS_NUMBER);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // NOTE: You should deal with number format exceptions here (use try-catch)
                float firstValue = Float.parseFloat(first.getText());
                float secondValue = Float.parseFloat(second.getText());
                result.setText(Float.toString(firstValue * secondValue));
            }
        });
    }   
}
于 2013-06-10T05:48:51.440 に答える