开发者

2 digit calculations wont display answer

I'm trying to do a simple, 2 digit multiplication screen and im not able to get it to actually display the answer. I know its probably something small and stupid. And since i'm kind of confused with the EditText/TextView uses, i wouldn't be surprised if it is something with those. Here is what I have so far:

import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.os.Bundle;
import android.view.View;

public class Multiply extends Index

{

    private EditText amount1;
    private EditText amount2;
    private TextView answer;
    private Button calculate;
    private double x=0;
    private double y=0;
    private double z=0;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calc);
        initControls();
    }

    public void initControls()
    {
          amount1=(EditText)findViewById(R.id.amount1);
          amount2=(EditText)findViewById(R.id.amount2);
          answer=(TextView)findViewById(R.id.answer);
          calculate=(Button)findViewById(R.id.calculate);
          calculate.setOnClickListener(new Button.OnClickListener()
          {
              public void onClick(View  v) 
              { 
                  calculate();
              }
          }
          );
     }
     private void calculate()
     {
          x=Double.parseDouble(amount1.getText().toString());
          y=Double.parseDouble(amount2.getText().toString());
开发者_JAVA技巧          z= x*y;
          answer.setText(Double.toString(z));

     }
} 

The "meat" of the XML file to accompany this looks like:

   <EditText 
        android:id="@+id/amount1" 
        android:layout_height="wrap_content" 
        android:layout_width="50dip">
    </EditText>

    <EditText 
        android:id="@+id/amount2" 
        android:layout_height="wrap_content" 
        android:layout_width="50dip">
    </EditText>

    <Button 
        android:id="@+id/calculate"
        android:text="CALCULATE" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </Button>

    <TextView 
        android:id="@+id/answer"
        android:layout_height="wrap_content" 
        android:layout_width="50dip">
    </TextView>


Your code is perfectly alright and it is working

Just change extends Index to extends Activity

package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Multiply extends Activity

{

private EditText amount1;
private EditText amount2;
private TextView answer;
private Button calculate;
private double x=0;
private double y=0;
private double z=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initControls();
}

public void initControls()
{
      amount1=(EditText)findViewById(R.id.amount1);
      amount2=(EditText)findViewById(R.id.amount2);
      answer=(TextView)findViewById(R.id.answer);
      calculate=(Button)findViewById(R.id.calculate);
      calculate.setOnClickListener(new Button.OnClickListener()
      {
          public void onClick(View  v) 
          { 
              calculate();
          }
      }
      );
 }
 private void calculate()
 {
      x=Double.parseDouble(amount1.getText().toString());
      y=Double.parseDouble(amount2.getText().toString());
      z= x*y;
      answer.setText(Double.toString(z));

 }
}   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜