2013-04-13 25 views
0

我目前是Android Development的新手,我在尝试在MainActivity Android类中使用自制类时遇到困难。 让我举个例子; 我创建了一个SquareArea类和想用它在MainActivity类我想在android中使用自制类

public class SquareArea{ 

    private double _length; 
    private double _width; 

    public SquareArea(double length , double width){ 
    _length = length; 
    _width = width; 
     area(); 
    } 

    private double area(){ 
    return _length 
    } 
} 

当我在MainActivity类别实例化SquareClass我希望能够使用的区域()方法和返回从提取时的值(EditText)
我想要使用该值以便将其放置在文本视图中;但是,这似乎不会发生。

我可以用方法做到,但我想用我自己的类代替。

请帮忙,我对此感到沮丧。

///Below is my MainActivity Class/// 
public class MainActivity extends Activity { 

    EditText mEditText1; 
    EditText mEditText2; 
    EditText mEditText3; 
    TextView mTextView; 
    Button mButton; 
    Double value1, value2; 
    SquareArea sq1; 

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

     mEditText1 = (EditText) findViewById(R.id.editText1); 
     mEditText2 = (EditText) findViewById(R.id.editText2); 
     mEditText3 = (EditText) findViewById(R.id.editText3); 
     mTextView = (TextView) findViewById(R.id.textView1); 

     mEditText1.setBackgroundColor(Color.GREEN); 
     mEditText2.setBackgroundColor(Color.GREEN); 
     mEditText3.setBackgroundColor(Color.RED); 



     mButton = (Button) findViewById(R.id.button1); 

     mButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       //When the button is clicked, call the calucate method. 
       // calculate(); 
       try { 
        value1 = Double.parseDouble(mEditText1.getText().toString()); 
        value2 = Double.parseDouble(mEditText2.getText().toString()); 
        sq1 = new SquareArea(value1, value2); 

        mTextView.setText(sq1.toString()); 
       } catch (NumberFormatException e) { 
        mTextView.setText("Please use numbers"); 
       } 
      }); 
     } 
} 

回答

0

你声明area() private,所以你不能把它

public double area(){ 
    return _length 
    } 

,你只需要调用您的区域()

mTextView.setText(String.ValueOf(sq1.area())); 
+0

谢谢@Hoan我会试试这个。我很感激。 – HRo

+0

@ HoanNguyen.Thank你。每件事都按预期工作。我真的很感谢你的帮助。 – HRo

0

我看到几个问题

mTextView.setText(sq1.toString()); 

toString m SquareArea中的方法不会被覆盖。所以的toString将返回关于(在内存中的位置,名称等)对象的默认信息

public SquareArea(double length , double width){ 
    _length = length; 
    _width = width; 
     area(); 
} 

在上面的代码中,你是在构造函数中。你为什么从这里打电话给area()?一个构造函数不能返回任何东西,并且area()返回一个double,所以area()的结果正在丢失。

最后一点:

private double area(){ 
    return _length 
} 

该方法是私有的。它不能从类/对象外调用。

为了使您的代码工作,这是我将改变。

public class SquareArea{ 

    private double _length; 
    private double _width; 

    public SquareArea(double length , double width){ 
    _length = length; 
    _width = width; 
    } 

    public double area(){ 
    return _length * _width; 
    } 
} 



///Below is my MainActivity Class/// 
public class MainActivity extends Activity { 

    EditText mEditText1; 
    EditText mEditText2; 
    EditText mEditText3; 
    TextView mTextView; 
    Button mButton; 
    Double value1, value2; 
    SquareArea sq1; 

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

     mEditText1 = (EditText) findViewById(R.id.editText1); 
     mEditText2 = (EditText) findViewById(R.id.editText2); 
     mEditText3 = (EditText) findViewById(R.id.editText3); 
     mTextView = (TextView) findViewById(R.id.textView1); 

     mEditText1.setBackgroundColor(Color.GREEN); 
     mEditText2.setBackgroundColor(Color.GREEN); 
     mEditText3.setBackgroundColor(Color.RED); 



     mButton = (Button) findViewById(R.id.button1); 

     mButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       //When the button is clicked, call the calucate method. 
       // calculate(); 
       try { 
        value1 = Double.parseDouble(mEditText1.getText().toString()); 
        value2 = Double.parseDouble(mEditText2.getText().toString()); 
        sq1 = new SquareArea(value1, value2); 

        mTextView.setText(String.ValueOf(sq1.area())); 
       } catch (NumberFormatException e) { 
        mTextView.setText("Please use numbers"); 
       } 
      }); 
     } 
}