自定义视图如何访问其活动?

发布于 2024-12-09 09:56:27 字数 128 浏览 0 评论 0原文

我有一个自定义视图,想要访问其活动中可用的字符串。我见过在视图类中使用 getContext() 的代码,但是没有方法可以访问通过 Intent 对其活动可用的 String。如何使活动中的字符串可用于其自定义视图?

I have a custom view and would like to access a String that is available in its activity. I've seen code that uses getContext() in the view class, but there is no method to access the String that has been made available to its activity via an intent. How to make a String in an activity available to its custom view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚雾 2024-12-16 09:56:27

View 类中的 getContext() 方法返回传递给其构造函数的上下文。通常这就是您想要的活动(活动扩展了上下文)。
所以这可能适合你:

Java:

((Activity)getContext()).someMethod(...);

Kotlin:

(context as? Activity)?.someMethod(...)

The getContext() method in the View class returns the context that was passed on its constructor. Usually that's the Activity you want (Activity extends Context).
So this probably works for you:

Java:

((Activity)getContext()).someMethod(...);

Kotlin:

(context as? Activity)?.someMethod(...)
治碍 2024-12-16 09:56:27

我仍然是 java 和 android 的菜鸟,所以这可能不起作用,但我只是想提供帮助。

您应该能够使用“putExtra”和“getExtra”将参数传递给意图/活动,例如:

在您的主要活动中:

 Intent EditDebtActivity = new Intent(getBaseContext(), EditDebt.class);
        EditDebtActivity.putExtra(DbAdapter.KEY_ROWID,dRowID);
        EditDebtActivity.putExtra(DbAdapter.KEY_DEBT, dName);
        EditDebtActivity.putExtra(DbAdapter.KEY_STARTINGAMOUNT, dStartAmount);
        EditDebtActivity.putExtra(DbAdapter.KEY_CURRENTAMOUNT, dCurrentAmount);
        EditDebtActivity.putExtra(DbAdapter.KEY_DUEDATE, dDueDate);
        EditDebtActivity.putExtra(DbAdapter.KEY_INTERESTRATE, dInterestRate);
        EditDebtActivity.putExtra(DbAdapter.KEY_MINPAYMENT, dMinPayment);
        startActivity(EditDebtActivity);

然后在新视图中的“onCreate”方法中,使用以下内容:

 Bundle extras = getIntent().getExtras();
  dRowID = extras.getLong(DbAdapter.KEY_ROWID);
  String rowidname = extras.getString(DbAdapter.KEY_DEBT);
  currentamount = extras.getDouble(DbAdapter.KEY_CURRENTAMOUNT);
  startingamount = extras.getDouble(DbAdapter.KEY_STARTINGAMOUNT);
  duedate = extras.getInt(DbAdapter.KEY_DUEDATE);

显然我已经使用了我自己的代码在这里,但当用户单击按钮时,我将行信息传递到新活动中。

有关使用 putextra 和 getextra 的更多信息,请参阅此链接:使用 Intent.putExtra 发送数组

希望这有帮助,这是我对社区的第一个贡献:)

I'm a noob to java and android still, so this may not work, but I'm just trying to help.

You should be able to pass in parameters to intents/activities using "putExtra" and "getExtra", for example:

In your main activity:

 Intent EditDebtActivity = new Intent(getBaseContext(), EditDebt.class);
        EditDebtActivity.putExtra(DbAdapter.KEY_ROWID,dRowID);
        EditDebtActivity.putExtra(DbAdapter.KEY_DEBT, dName);
        EditDebtActivity.putExtra(DbAdapter.KEY_STARTINGAMOUNT, dStartAmount);
        EditDebtActivity.putExtra(DbAdapter.KEY_CURRENTAMOUNT, dCurrentAmount);
        EditDebtActivity.putExtra(DbAdapter.KEY_DUEDATE, dDueDate);
        EditDebtActivity.putExtra(DbAdapter.KEY_INTERESTRATE, dInterestRate);
        EditDebtActivity.putExtra(DbAdapter.KEY_MINPAYMENT, dMinPayment);
        startActivity(EditDebtActivity);

Then in the "onCreate" method in your new view, use the following:

 Bundle extras = getIntent().getExtras();
  dRowID = extras.getLong(DbAdapter.KEY_ROWID);
  String rowidname = extras.getString(DbAdapter.KEY_DEBT);
  currentamount = extras.getDouble(DbAdapter.KEY_CURRENTAMOUNT);
  startingamount = extras.getDouble(DbAdapter.KEY_STARTINGAMOUNT);
  duedate = extras.getInt(DbAdapter.KEY_DUEDATE);

obviously I've used my own code here, but I am passing row information into a new activity when a user clicks a button.

See this SO link for more info on using putextra and getextra: Sending arrays with Intent.putExtra

Hope this helps, it's my first contribution back to the community :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文