Android - 进行简单的计算

时间:2016-02-17 16:00:10

标签: java android-studio

我有 EditText TextView 按钮。用户在 EditText 中键入一个数字。然后,当用户点击按钮并在 TextView 中显示答案时,我希望将其除以100。

这是我的布局( activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<EditText
    android:id="@+id/tv_numb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="77dp"
    android:hint="Provide a number"/>

<Button
    android:id="@+id/btn_doCalc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Calculate"
    android:layout_below="@+id/tv_numb"
    android:layout_centerHorizontal="true" />

<TextView
    android:id="@+id/tv_answer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn_doCalc"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:textSize="30sp"
    android:text="Answer" />

MainActivity.java

public class MainActivity extends ActionBarActivity {

private EditText editTextNumb;
private Button buttonCalc;
private TextView tvAnswer;


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

    editTextNumb = (EditText) findViewById(R.id.tv_numb);
    buttonCalc = (Button) findViewById(R.id.btn_doCalc);
    tvAnswer = (TextView) findViewById(R.id.tv_answer);

    //I think this is where my problem is-------
    final int result = (editTextNumb.getText().toString())/100;
    //

        buttonCalc.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                tvAnswer.setText(result);

            }});
    }

}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:3)

Button   mButton;
EditText mEdit;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mButton = (Button)findViewById(R.id.button);
    mEdit   = (EditText)findViewById(R.id.edittext);

    mButton.setOnClickListener(
        new View.OnClickListener()
        {
          public void onClick(View view)
          {
              try
              {
                 int result = Integer.parseInt(mEdit.getText().toString())/100;
                // show it to them
                Log.v("EditText", "result is "+result);

                Toast.makeText(getApplicationContext(),"result is "+result,     
                               Toast.LENGTH_LONG).show();
              }
              catch (final NumberFormatException e) 
              {
                  // tell them they didnt enter a valid number
                 Toast.makeText(getApplicationContext(),"Please Enter a valid number",     
                               Toast.LENGTH_LONG).show();
              }
          }
    });
}