gpt4 book ai didi

java - 如何从 Phonegap 插件更改 native 元素?

转载 作者:行者123 更新时间:2023-11-30 09:26:46 25 4
gpt4 key购买 nike

我想更改从 cordova 插件传递的 native TextView 值。此应用程序将在一个 Activity 布局中显示 2 个元素 - native 元素 (TextView) 和 cordova webview。是否可以在 main 和 phonegap 之间交换上下文以更新当前 UI ...有人知道吗?...

示例概念-

public class MainActivity extends Activity implements CordovaInterface{
.....
.....
public void changeText(String txt){
textView = (TextView) findViewById(R.id.textView);
textView.setText(txt);

}

}

Cordova 插件:

公共(public)类 MyPlugin 扩展 CordovaPlugin {

 public boolean execute(String action, JSONArray args, final CallbackContext callbackContext){
if (action.equals("changeText")) {
..............
}
}
}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<org.apache.cordova.CordovaWebView
android:id="@+id/tutorialView"
android:layout_width="match_parent"
android:layout_height="196dp" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</RelativeLayout>

最佳答案

终于成功了.... 2 个元素很重要:

  • cordova context- 在插件类中调用 textview 使用:

TextView textView =
cordova.getActivity().findViewById(R.id.textView);

  • thread - 使用 runOnUiThread 调用 cordova 插件执行中的更改

例如:-

cordova.getActivity().runOnUiThread(new Runnable(){
public void run() {

textView.setText("Cordova Hello World");

callbackContext.success();

// Thread-safe.

} });

关于java - 如何从 Phonegap 插件更改 native 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14845775/

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