java - 如何从 Activity 调用自定义 View 中的函数

标签 java android button android-custom-view

使用MainActivity中的按钮如何调用必须运行的sprite.move("left")(这将每秒向左移动 Sprite 两次)在GView中?

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);  
        Button button = (Button) findViewById(R.id.button);
        button.setOnTouchListener(new View.OnTouchListener() {

            private Handler mHandler;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (mHandler != null)
                        return true;
                    mHandler = new Handler();
                    mHandler.postDelayed(mAction, 0);
                    break;
                case MotionEvent.ACTION_UP:
                    if (mHandler == null)
                        return true;
                    mHandler.removeCallbacks(mAction);
                    mHandler = null;
                    break;
                }
                return false;
            }

            Runnable mAction = new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "Performing action...", Toast.LENGTH_LONG).show();
                    mHandler.postDelayed(this, 500);
                }
            };

        });
    }
}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >

<com.viracide.depth.GView
   android:id="@+id/gview"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_alignParentLeft="true"
   android:layout_alignParentRight="true"
   android:layout_alignParentTop="true"
   android:layout_marginBottom="180dp"
   android:layout_marginLeft="40dp"
   android:layout_marginTop="40dp"
   android:layout_marginRight="40dp">
</com.viracide.depth.GView>

<Button
   android:id="@+id/button"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@+id/gview"
   android:layout_alignParentBottom="true"
   android:layout_marginBottom="73dp"
   android:text="Button" />

</RelativeLayout>

GView.java

public class GView extends View {
    private Bitmap bmp;
    sprite sprite; //sprite image

    public GView(Context context) {
        super(context);
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.bit);
    }

    public void draw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bmp, 10 , 10, null);
    }
}

最佳答案

public class MainActivity extends Activity {
GView mGView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);  
    // initialize here
    mGView = (GView)findViewById(R.id.gview);
    // put this anywhere and make sure you do not violate UI thread constraint for making any UI changes
    mGview.<yourmethod>()
 ...

关于java - 如何从 Activity 调用自定义 View 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24353559/

相关文章:

java - Postgres 9.5 : Auto increment after migrating from mysql

java - 用于多个箭头键按下的 KeyBinding

android - android中如何计算密度值

java - SharedPreferences 不会发送位图 : Failed to allocate memory

android - 如何在 Android 2.1 中启动 Activity

java - 上传文件excel并用java中的JAX-RS 2.0读取

java - 使用toString方法打印ArrayList内容时出错

android - 使用套接字从Android到NodeJS服务器的多个参数

java - 使用 JButton 中断线程

android - 将按钮的位置设置为屏幕上的随机点? - 安卓