Java 如何从Phonegap插件更改本机元素?

Java 如何从Phonegap插件更改本机元素?,java,android,cordova,phonegap-plugins,Java,Android,Cordova,Phonegap Plugins,我想更改从cordova插件传递的本机TextView值。此应用程序将在一个活动布局中显示2个元素—原生元素(TextView)和cordova webview。是否可以在main和phonegap之间交换上下文以更新当前UI。。。 有人知道吗 范例概念- public class MainActivity extends Activity implements CordovaInterface{ ..... ..... public void changeText(String txt)

我想更改从cordova插件传递的本机TextView值。此应用程序将在一个活动布局中显示2个元素—原生元素(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插件:

公共类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>

最后,它起作用了……两个要素很重要:

  • cordova上下文-使用以下命令在插件类中调用textview:
TextView TextView=
cordova.getActivity().findViewById(R.id.textView);

  • 线程-使用runOnUiThread调用cordova插件执行中的更改
例如:-


你能分享你的代码吗?我想知道你是如何在你的应用程序中实现原生和HTML组件的。
cordova.getActivity().runOnUiThread(new Runnable(){
 public void run() {

                textView.setText("Cordova Hello World"); 

                callbackContext.success(); 

                // Thread-safe. 

        } });