gpt4 book ai didi

java - 如何在Android Studio中使用View Parameter调用方法

转载 作者:行者123 更新时间:2023-12-01 11:31:23 30 4
gpt4 key购买 nike

如何从操作栏中的菜单调用此方法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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com