我想更改我从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>

最佳答案

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


Cordova上下文调用插件类中的textview使用:


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


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


例如:-

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

                textView.setText("Cordova Hello World");

                callbackContext.success();

                // Thread-safe.

        } });

07-24 09:48
查看更多