Android按钮无法移动?

时间:2015-10-17 22:06:09

标签: java android error-handling

我正在按照在线教程进行按钮更改位置(从左上角到右下角),同时增加尺寸。我是使用transitionmanager做的。

TransitionManager.beginDelayedTransition(mylayout);

然后我想知道我是否可以从右下角到左上角做动画(这不包含在教程中,我只是出于纯粹的好奇心尝试了这一点)。我创建了另一个名为

的方法
moveButtonBack();

每次调用moveButton或moveButtonBack()时,x变量都会增加1,如果x是偶数则调用moveButton,如果x是奇数,则调用moveButtonBack。顺便说一句,我在代码中添加了很多注释来跟踪事物。要在moveButtonBack中更改按钮大小,我使用变量H和W作为显示按钮的第一个高度和宽度。

问题

对我来说,代码似乎很好,我无法确定问题。当我在手机上运行时,按钮会发疯。文本在按钮上快速变化,并且以无组织的方式移动,或者当我触摸时移动到中心,当我放开时移动到顶部/底部,或者它保持在中心并且看起来好像在振动(迅速上下移动)。它具有300的恒定高度和450的宽度,而不是收缩和扩大。我不知道问题是什么,如果我得到帮助,我会非常感激。我花了一个多小时在代码中插入注释,并尝试通过更改听众,高度等内容来解决问题。

    package com.example.my.transitions;

import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.*;
import android.widget.*;
import android.transition.*;
import android.support.v7.app.*;

public class MainActivity extends AppCompatActivity {

ViewGroup myslayout;
View mysButton; //Used in move and moveback
int x = 0;
double h;
double w;

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

    myslayout = (ViewGroup) findViewById(R.id.myslayoutid);

    myslayout.setOnTouchListener(
            new RelativeLayout.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {

                        if (x%2 == 0) {

                            moveButton();
                            return true;

                        }
                    else{

                            moveButtonBack();
                            return true;
                        }

                    return true; //touch listener as been handled
                }
            }
    );
}

public void moveButton() {
   Button mysButton = (Button) findViewById(R.id.mysbuttonid);

    h = mysButton.getHeight();
    w = mysButton.getWidth();


    TransitionManager.beginDelayedTransition(myslayout);

    //////////////////////Change position of button////////////////////////////


    RelativeLayout.LayoutParams positionRules = new RelativeLayout.LayoutParams(

            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

  positionRules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.ALIGN_PARENT_BOTTOM);

    positionRules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.ALIGN_PARENT_RIGHT);


    mysButton.setLayoutParams(positionRules); 

    /////////////////////////Change size of button////////////////////////////


    ViewGroup.LayoutParams sizerules = mysButton.getLayoutParams();
    sizerules.width = 450;
    sizerules.height = 300;

    mysButton.setLayoutParams(sizerules);

    mysButton.setText("Height: " + sizerules.height + "Width: " + sizerules.width);
    x++; //Make x odd, to be called as moveButtonBack next time
}

    public void moveButtonBack(){
        Button mysButton =(Button) findViewById(R.id.mysbuttonid);
       TransitionManager.beginDelayedTransition(myslayout);

        RelativeLayout.LayoutParams positionBackRules = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        positionBackRules.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.ALIGN_PARENT_TOP);
        positionBackRules.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.ALIGN_PARENT_LEFT); //Move back to top left

        mysButton.setLayoutParams(positionBackRules);

        ViewGroup.LayoutParams sizebackrules = mysButton.getLayoutParams();
        sizebackrules.height = (int) h;
        sizebackrules.width = (int) w;
        //I am typcasting h and w into ints in case it is a double, since .width and .height only take ints.

    mysButton.setLayoutParams(sizebackrules);

        mysButton.setText("Height: " + sizebackrules.height + "Width: " + sizebackrules.width + "h " + h + "w" + w);

        x++; //Make x even again


    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

同样,我真的很感激答案,因为我已经在这个问题上待了几个小时了!

1 个答案:

答案 0 :(得分:1)

if (head == NULL || head->data == '\0') 方法中,检查是否触及或触摸。只为其中一个事件编写移动按钮逻辑。

例如:

onTouch

有关Android Documentation

的更多详情

关于按钮大小不变的问题: 将if (event.getAction() == MotionEvent.ACTION_DOWN) { // Code for Move button / Move button back } h的计算移至活动w方法可能有所帮助。