【问题标题】:How to pass data from activity to class in android? [closed]如何将数据从活动传递到android中的类? [关闭]
【发布时间】:2017-03-25 05:57:28
【问题描述】:

如何将数据从活动传递到类。 ?
下面提到了我的锁屏类。我想将活动中的数据传递给这个锁屏类。

请任何人帮助我?

Lock Screen class
*****************

public class LockScreen extends Activity{

public WindowManager winManager;
public RelativeLayout wrapperView;

public int background;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    }

主要活动


  public static int   backgroundPics;        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

    }

My problem is I didn't get the updated value of background in Lockscreen class. It is always shows 0. 

【问题讨论】:

  • 在自定义 View 类中创建一个 public 方法,该类采用 int/Integer 参数。
  • 我在这里没有看到自定义视图。它是一个活动。你有活动的构造函数。那很糟。您需要阅读有关活动和自定义视图的 android 文档
  • @Raghunandan - 我想将更新后的数据从活动传递到非活动类
  • @Raghunandan :我已经更新了我的完整代码。请帮帮我
  • 你从哪里管理 Mainactivity.backgroundPics

标签: android android-layout android-activity android-custom-view


【解决方案1】:
Create a class for your requirement as CustomView and define public method to pass integer value and use it there. As I have created setIntValue().

    public class CustomView extends View {
    private int mIntValue;

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(Context context) {
        super(context);
    }

    public void setIntValue(int value) {
        //Like this you can use this integer value
        //this.mIntValue = value;
        //this.setText(value);
    }
}

用法: 1。直接用CustomView类实例:

CustomView mView = new CustomView(this);
mView.setIntValue(100);

2。在布局 xml 中使用 CustomView:

在你的布局 xml-

<your_app_package_for_custom_view_class.CustomView
        android:id="@+id/customview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

在您的活动中-

CustomView mView = (CustomView)findViewById(R.id.customview);
mView.setIntValue(100);

【讨论】:

  • 我试过这个,但我没有得到解决方案。我的问题是“如何将数据从活动传递到 android 中的自定义视图?”
  • 但我看到你的问题是“如何将整数值从主要活动传递到 android 中的自定义视图?”与stackoverflow.com/questions/43013074/…相关
  • 这里的数据是什么意思?数据是什么?
  • 这里的Data表示一个整数值。在这里,当我编写这些构造函数时,它会显示一个错误,例如 "Activity() in Activity cannot be applied" 。我已经使用 WindowManager.addview 创建了 View 并且没有扩展 View 。对不起,如果我的英语不好
  • 展示你的代码你是怎么做的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多