【问题标题】:Changing UI elements in Chaquopy在 Chaquopy 中更改 UI 元素
【发布时间】:2020-05-13 17:06:10
【问题描述】:

我目前正在尝试从 Python 模块中找出如何在 MainActivity(UI 是用 Java 编写)中更改我的 textview 的文本。我知道我必须以某种方式将我的 mainactivity 传递给我的 python 模块,但我仍然无法弄清楚如何正确地做到这一点。 我正在使用 Chaquopy SDK 进行 python 交互。

我现在的主要活动是这样的

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //set the layout
        final TextView simpleTextView = (TextView) findViewById(R.id.simpleTextView); //get the id for TextView
        Button changeText = (Button) findViewById(R.id.btnChangeText); //get the id for button
        changeText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                simpleTextView.setText("After Clicking"); //set the text after clicking button
            }
        });
    }


}

现在我需要一个 python 函数来操作文本视图。

提前致谢。

【问题讨论】:

    标签: python android chaquopy


    【解决方案1】:

    总之,您需要做的是:

    具体来说,您可以:

    • 让 Python 函数返回一个字符串,然后让 Java 代码将它传递给 setText。例如setText(mod.callAttr(...).toString()),其中modgetModule的返回值。

    或者:

    • 使用callAttr 将TextView 对象传递给Python 函数,然后让Python 代码调用setText。例如,如果您的 Python 函数名为 f,那么您将编写 mod.callAttr("f", simpleTextView),该函数将接收 TextView 作为其参数。

    【讨论】:

    • 首先谢谢!您显示的最后一个选项看起来适合我。我需要在 python 端导入什么?在 python 上设置 textview 文本的等效项如何?
    • 我需要在 python 端导入什么? – 什么都没有。
    • 在 python 上设置 textview 文本的等效项如何? – 与在 Java 中相同,因为两种语言都有相似的方法调用语法。有关示例,请参阅here
    • 比我想象的还要容易,感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 2020-06-25
    • 2019-11-01
    相关资源
    最近更新 更多