0

我对编程很陌生,我试图自学如何为安卓设备编写应用程序。我目前正在使用官方 android 开发者网站上的原始用户指南。我试图从用户那里获取整数输入,一旦单击按钮,该数字将乘以 .1 *(取自第二个用户编辑文本框的数字)。例如,在第一个文本框中,用户将输入 100,在第二个文本框中,用户将输入 10。第三个文本框中的结果将是 10/100*100,即 10。我将如何去做这个和确保结果显示在第三个文本框中。

这是我到目前为止的代码

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 回答