我正在使用Embeddinator-4000和Xamarin在Visual C#中进行编码并传递给Android Studio。我想做一个简单的程序。 https://github.com/mono/Embeddinator-4000 我按照这里的教程将aar文件导入android studio。
在Visual Studio Xamarin中: 我已经执行了
的代码namespace CalculatorLibrary
{
[Activity(Label = "CalculatorActivity")]
public class CalculatorActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//SetContentView(Resource.Layout.layout1);
}
public double Add(double num1, double num2)
{
double sum;
sum = num1 + num2;
return sum;
}
}
}
我有一个aar文件的输出,然后导入到我的android studio中。我想在android studio中调用我的Add函数。
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import calculatorlibrary.*;
import md54223a39312377e7a37c3fe640756f5f0.CalculatorActivity;
public class MainActivity extends AppCompatActivity {
Button addButton;
EditText num1text, num2text;
TextView result;
String num1string, num2string;
double num1, num2, ans;
CalculatorActivity cal = new CalculatorActivity();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButton = findViewById(R.id.addbutton);
num1text = findViewById(R.id.num1editText);
num2text = findViewById(R.id.num2editText);
result = findViewById(R.id.resulttextView);
}
@Override
protected void onResume()
{
super.onResume();
//Intent intent = new Intent(this, CalculatorActivity.class);
//startActivity(intent);
addButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
num1string = num1text.getText().toString();
num2string = num2text.getText().toString();
num1 = Double.parseDouble(num1string);
num2 = Double.parseDouble(num2string);
cal.Add(num1,num2); //I want to call this, but it can't find the Add() function.
//ans = Add(num1, num2);
//result.setText(Double.toString(ans));
}
});
}
答案 0 :(得分:0)
如果我理解正确,则您要构建库并要使用它? 如果是这样,请按照以下步骤操作:
aar
文件settings.gradle
,在行include ':app', ':your-lib-name'
中添加implementation project(':your-lib-name')
答案 1 :(得分:0)
首先,导入语句
import android.*yourmodule*;
第二,按如下所示创建methodA.java的对象:
yourmodule methodA = new yourmodule();
最后,您调用该方法的函数。
methodA.Calculate()