我目前是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");
}
});
}
}
谢谢@Hoan我会试试这个。我很感激。 – HRo
@ HoanNguyen.Thank你。每件事都按预期工作。我真的很感谢你的帮助。 – HRo