如何在 Android Studio 中使用视图参数调用方法

How to call method with View Parameter in Android Studio

如何从操作栏的菜单中调用此方法 Calculate()

应用objective是计算以下公式: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() {
...

如果你想从一个视图执行方法,那么你需要参数视图,例如:

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);
    }
}