在activity_xml文件下绘制界面
在java class下声明控件
变量与控件进行匹配
页面跳转
Intent intent = new Intent(MainActivity.this, classactivity.class);
String message = edit1.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
数据存储、读取
//存数据
SharedPreferences sharedPreferences_time= getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor_time = sharedPreferences_time.edit();
editor_time.putString("time", time.toString());
editor_time.apply();
SharedPreferences sharedPreferences=getSharedPreferences("MyPrefs",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putBoolean("islogin",true);
editor.apply();
finish();
//读数据
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
String str = sharedPreferences.getString("data",null);
SharedPreferences sharedPreferences2 = getSharedPreferences("MyPrefs", MODE_PRIVATE);
int Linstener_page = sharedPreferences2.getInt("flag",0);
调用Python程序
PyObject callPythonCode() {
Python python=Python.getInstance(); // 初始化Python环境
PyObject pyObject=python.getModule("login");//"text"为需要调用的Python文件名
PyObject res=pyObject.callAttr("webstart",new Kwarg("username",edit1.getText().toString()),new Kwarg("password",edit2.getText().toString()));//"sayHello"为需要调用的函数名
return res;
// Python.getInstance() : 得到python的对象
// py.getModule(“xxx”) : 是调用名为xxx的.py文件
// callAttr(“photo”,new Kwarg(“jpg”,paths)): 第一个参数表示调用的python中的方法名photo,后面是传入方法的参数(jpg为方法的参数名,paths为传入的变量)
// obj.toJava 将PyObject类型转为java中的类型
}
在build.gradle文件中添加
buildscript {
repositories {
google()
mavenCentral()
maven { url "https://chaquo.com/maven" }
}
dependencies {
classpath "com.android.tools.build:gradle:8.5.0"
classpath "com.chaquo.python:gradle:12.0.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
然后同步gradle之后创建python文件即可
log日志
Log.d("tag", "this is test" );