0

在我的应用程序中有九个按钮。当一个按钮被触摸时,它应该移动到一个目标位置。如果使用图像而不是按钮,这可以很容易地完成surfaceview。如何用按钮做到这一点?

编辑

package com.amit.wozzle;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
private Button b7;
private Button b8;
private Button b9;
private int xb=3;
private int yb=3;
private int xbl;
private int ybt;
private int x=3;
private int y=3;
private TextView tv55; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b1 = (Button) findViewById(R.id.tv1);
    b2 = (Button) findViewById(R.id.tv2);
    b3 = (Button) findViewById(R.id.tv3);
    b4 = (Button) findViewById(R.id.tv4);
    b5 = (Button) findViewById(R.id.tv5);
    b6 = (Button) findViewById(R.id.tv6);
    b7 = (Button) findViewById(R.id.tv7);
    b8 = (Button) findViewById(R.id.tv8);
    b9 = (Button) findViewById(R.id.tv9);

    b2.setOnClickListener(new Glide());
    b3.setOnClickListener(new Glide());
    b4.setOnClickListener(new Glide());
    b5.setOnClickListener(new Glide());
    b6.setOnClickListener(new Glide());
    b7.setOnClickListener(new Glide());
    b8.setOnClickListener(new Glide());
    b9.setOnClickListener(new Glide());



}


class Glide implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        int[] loc ={0,0};
        b1.getLocationOnScreen(loc);
        int xw = b1.getWidth();

        int[] loc1={0,0};
        v.getLocationOnScreen(loc1);


        TableLayout.LayoutParams lp = (TableLayout.LayoutParams) v.getLayoutParams();
        lp.leftMargin = loc[0];
        lp.topMargin = loc[1];
        v.setLayoutParams(lp);
        v.invalidate();


    }


}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


}

这导致强制关闭。Logcat 说

07-14 02:04:32.564: D/AndroidRuntime(310): Shutting down VM
07-14 02:04:32.564: W/dalvikvm(310): threadid=1: thread exiting with uncaught exception  (group=0x4001d800)
07-14 02:04:32.574: E/AndroidRuntime(310): FATAL EXCEPTION: main
07-14 02:04:32.574: E/AndroidRuntime(310): java.lang.ClassCastException:     android.widget.TableRow$LayoutParams
07-14 02:04:32.574: E/AndroidRuntime(310):  at com.amit.wozzle.MainActivity$Glide.onClick(MainActivity.java:81)
07-14 02:04:32.574: E/AndroidRuntime(310):  at android.view.View.performClick(View.java:2408)
07-14 02:04:32.574: E/AndroidRuntime(310):  at android.view.View$PerformClick.run(View.java:8816)
07-14 02:04:32.574: E/AndroidRuntime(310):  at android.os.Handler.handleCallback(Handler.java:587)
07-14 02:04:32.574: E/AndroidRuntime(310):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 02:04:32.574: E/AndroidRuntime(310):  at android.os.Looper.loop(Looper.java:123)
07-14 02:04:32.574: E/AndroidRuntime(310):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-14 02:04:32.574: E/AndroidRuntime(310):  at java.lang.reflect.Method.invokeNative(Native Method)
07-14 02:04:32.574: E/AndroidRuntime(310):  at java.lang.reflect.Method.invoke(Method.java:521)
07-14 02:04:32.574: E/AndroidRuntime(310):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-14 02:04:32.574: E/AndroidRuntime(310):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-14 02:04:32.574: E/AndroidRuntime(310):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

我认为您缺少的只是将参数设置在您希望按钮出现的位置。以下是示例:

public class MyAppActivity extends Activity {    

TableLayout mLayout;
Button b1;  

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);       

    b1 = (Button) findViewById(R.id.viewMatched_btn);        
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            TableLayout.LayoutParams layoutParams = (TableLayout.LayoutParams) b1.getLayoutParams();
            layoutParams.topMargin = 200;
            layoutParams.leftMargin = 200;              
            b1.setLayoutParams(layoutParams);

            if(mLayout == null) {
                mLayout = (TableLayout) b1.getParent();
            }

            mLayout.invalidate();
        }
    });        

  }
}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/temps_holder"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/viewMatched_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"               
    android:text="Button"
    android:textColor="#ffffffff" />

</TableLayout>

希望有帮助。此外,如果您想要创建自定义动画,您可以看看这个

于 2012-07-14T02:20:52.760 回答