作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何从操作栏中的菜单调用此方法Calculate()
?
应用程序的目标是计算以下公式:A = (2X + F)/(X - 2F)
这是我的 java 代码
{
private EditText txtX;
private EditText txtF;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtX=(EditText)findViewById(R.id.X);
txtF=(EditText)findViewById(R.id.F);
}
public void Calculate() {
boolean ok = true;
double x = 0;
try {
x = leeDouble(txtX);
} catch (NumberFormatException e) {
ok = false;
}
double f = 0;
try {
f = leeDouble(txtF);
} catch (NumberFormatException e) {
ok = false;
}
if (ok) {
final double divisor = x - 2 * f;
if (Math.abs(divisor) > 0.0005) {
double a = (2 * x + f) / divisor;
message("a=" + a);
} else {
message("Wrong numbers");
}
}
}
private void message(String texto){
Toast.makeText(this,texto,Toast.LENGTH_SHORT).show();
}
private double leeDouble(EditText editText) throws NumberFormatException{
try{
return Double.parseDouble(editText.getText().toString().trim());
} catch(NumberFormatException e){
editText.setError("Wrong number");
throw e;
} }
在菜单 xml 文件中执行此操作
<item
android:id="@+id/action_calculate"
android:orderInCategory="100"
android:title="@string/text2"
app:showAsAction="ifRoom"
/>
当我尝试调用它时,它说它需要一个参数。
最佳答案
使用方法onOptionsItemSelected()
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_calculate) {
Calculate(); //Execute the method!
}
return super.onOptionsItemSelected(item);
}
但你不需要参数:
public void Calculate() {
...
如果你想从 View 执行该方法,那么你需要参数 View,例如:
public void Calculate(View Vista) {
....
从 Button 执行该方法,使用属性 android:onClick
<Button
android:id="@+id/myButton"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="Calcula"/>
这是完整的代码:
private EditText txtX;
private EditText txtF;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtX=(EditText)findViewById(R.id.X);
txtF=(EditText)findViewById(R.id.F);
}
public void Calculate() {
boolean ok = true;
double x = 0;
try {
x = leeDouble(txtX);
} catch (NumberFormatException e) {
ok = false;
}
double f = 0;
try {
f = leeDouble(txtF);
} catch (NumberFormatException e) {
ok = false;
}
if (ok) {
final double divisor = x - 2 * f;
if (Math.abs(divisor) > 0.0005) {
double a = (2 * x + f) / divisor;
message("a=" + a);
} else {
message("Wrong numbers");
}
}
}
private void message(String texto){
Toast.makeText(this,texto,Toast.LENGTH_SHORT).show();
}
private double leeDouble(EditText editText) throws NumberFormatException{
try{
return Double.parseDouble(editText.getText().toString().trim());
} catch(NumberFormatException e){
editText.setError("Wrong number");
throw e;
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_calculate) {
Calculate();
}
return super.onOptionsItemSelected(item);
}
}
关于java - 如何在Android Studio中使用View Parameter调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30354645/
我是一名优秀的程序员,十分优秀!