将文本从MainActivity传递到Kotlin中的子TextView



我以前没有使用过Android Studio和Kotlin,但是通过native从Flutter传递数据的任务出现了。我需要从MainActivity传递文本,我通过MethodChannel到AndroidTextView获取文本,然后传递给Flutter。我不知道如何传递文本,我试图这样做的意图,但似乎我做错了什么。

MainActivity

class MainActivity : FlutterActivity() {
private val androidViewId = "INTEGRATION_ANDROID"
private val methodChannel = "CALL_METHOD"
private val intentMessageId = "CALL"
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
flutterEngine.platformViewsController.registry.registerViewFactory(androidViewId, AndroidTextViewFactory(flutterEngine.dartExecutor.binaryMessenger, Intent()))
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, methodChannel).setMethodCallHandler {
call, result ->
if (call.method == intentMessageId) {
val intent = Intent()
intent.putExtra(intentMessageId, call.arguments.toString())
AndroidTextViewFactory(flutterEngine.dartExecutor.binaryMessenger, intent)
result.success(call.arguments);
} else {
result.notImplemented()
}
}
}
}

AndroidTextView

internal class AndroidTextView(context: Context?, id: Int, creationParams: Map<String?, Any?>?, messenger: BinaryMessenger, data: Intent) : PlatformView {
private val textView: TextView = TextView(context)
private val intentMessageId = "CALL"
override fun getView(): View {
return textView
}
override fun dispose() {}
init {
val intent = Intent(context, MainActivity::class.java);
val text = intent.getStringExtra(intentMessageId);
textView.textSize = 36f
textView.text = "$text"
}
}

AndroidTextViewFactory

class AndroidTextViewFactory(messenger: BinaryMessenger, data: Intent) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
private val binaryMessenger: BinaryMessenger = messenger
private val intent: Intent = data
override fun create(context: Context?, viewId: Int, args: Any?): PlatformView {
val creationParams = args as Map<String?, Any?>?
return AndroidTextView(context, viewId, creationParams, binaryMessenger, intent)
}
}

Intent,通常用作Explicit Intent,

显式意图,在同一应用程序中的两个活动之间进行通信。

意图是这样使用的:

class MainActivity : FlutterActivity() {
fun openActivityTwo(){
startActivity(Intent(this@MainActivity,ActivityTwo::class.java))
}
}
class ActivityTwo : FlutterActivity() {}

我看到你的代码非常像flutter教程:在你的flutter应用程序中托管原生Android视图与平台视图

如本教程所示,您不需要MethodChannel

MainActivity中注册android原生视图


class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
flutterEngine
.platformViewsController
.registry
.registerViewFactory("key", NativeViewFactory())
}

}

则用于dart


class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: Scaffold(
body: One(),
),
);
}
}
class One extends StatefulWidget {
const One({Key? key}) : super(key: key);
@override
State<One> createState() => _OneState();
}
class _OneState extends State<One> {
@override
Widget build(BuildContext context) {
// This is used in the platform side to register the view.
const String viewType = 'key';
// Pass parameters to the platform side.
const Map<String, dynamic> creationParams = <String, dynamic>{};
return PlatformViewLink(
viewType: viewType,
surfaceFactory: (context, controller) {
return AndroidViewSurface(
controller: controller as AndroidViewController,
gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
);
},
onCreatePlatformView: (params) {
return PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
onFocus: () {
params.onFocusChanged(true);
},
)
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
..create();
},
);
}
}

可以不使用channel方法处理相关数据

相关内容

  • 没有找到相关文章

最新更新