我想在android中使用一个自制的类
我目前是Android开发新手,在我的MainActivity Android类中尝试使用自制类时遇到了困难。 让我给你举个例子; 我创建了一个SquareArea类,并希望在MainActivity类中使用它我想在android中使用一个自制的类,android,Android,我目前是Android开发新手,在我的MainActivity Android类中尝试使用自制类时遇到了困难。 让我给你举个例子; 我创建了一个SquareArea类,并希望在MainActivity类中使用它 public class SquareArea{ private double _length; private double _width; public SquareArea(double length , double width){ _length = l
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时,我希望能够使用area()方法,并在从(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");
}
});
}
}
您声明了area()private
,因此不能调用它
public double area(){
return _length
}
你只需要打电话给你的区域()
我看到几个问题
mTextView.setText(sq1.toString());
SquareArea中的toString方法不会被重写。因此toString将返回关于对象的默认信息(内存中的位置、名称等)
在上面的代码中,您位于构造函数内部。你为什么从这里呼叫area()?构造函数无法返回任何内容,并且area()的结果将丢失,因为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");
}
});
}
}
谢谢@Hoan,我会试试的。谢谢。@HoanNguyen。谢谢。一切都如期进行。我真的很感谢你的帮助。
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");
}
});
}
}