如何将数据从活动传递到android中的类?

时间:2017-03-25 05:57:28

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

如何将数据从活动传递到类。 ?
My Lock屏幕类如下所述。我想将活动中的数据传递给此锁屏类。

请有人帮帮我吗?

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. 

1 个答案:

答案 0 :(得分:0)

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);