【问题标题】:How to access parent activity variable from SurfaceView?如何从 SurfaceView 访问父活动变量?
【发布时间】:2015-12-31 18:40:56
【问题描述】:

按照我的代码看起来像

activity_main.xml

...
<com.example.android.demo.MySurface
            android:id="@+id/gameSurface"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="5"/>
...

MainActivity.java

public class MainActivity extends Activity{
   boolean playerFlag = false;
...
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
...

MySurface.java

public class MySurface extends SurfaceView implements SurfaceHolder.Callback{
   public MySurface(Context context){
        super(context);
        this.context = context;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Integer index = getIndexFromCoord(event.getX(),event.getY());

        // I need to access the variable playerFlag here

        return true;
    }
...
}

我需要从MySurface class 的onTouchEvent() 访问变量playerFlag。有什么建议请...

【问题讨论】:

    标签: android android-activity surfaceview


    【解决方案1】:

    首先创建 attrs_your_view_name.xml

    <resources>
        <declare-styleable name="MyView">
           <attr name="playerflag" format="boolean" />
        </declare-styleable>
    </resources>
    

    然后在你的视图中类:

    public class MyView extends View {
        private boolean playerflag;
    
        public MyView(Context context) {
            super(context);
            init(null, 0);
        }
    
        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(attrs, 0);
        }
    
        public MyView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(attrs, defStyle);
        }
    
        private void init(AttributeSet attrs, int defStyle) {
            // Load attributes
            final TypedArray a = getContext().obtainStyledAttributes(
                    attrs, R.styleable.MyView, defStyle, 0);
    
            playerflag=a.getBoolean(R.styleable.MyView_playerflag,false);
    
        }
    
        public boolean isPlayerflag() {
            return playerflag;
        }
    
        public void setPlayerflag(boolean playerflag) {
            this.playerflag = playerflag;
        }
    }
    

    在 MainActivity 中:

    MyView myview=(MyView)findViewById(R.id.your_id);
    myview.setPlayerflag(this.playerflag);
    

    您现在也可以从 xml 中指定它:

    <com.example.myapp.MyView
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            app:playerflag="true"
            />
    

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 2012-01-13
      相关资源
      最近更新 更多