Android按钮不动?
我正在按照在线教程进行按钮更改位置(从左上角到右下角),同时增加尺寸。我正在使用transitionmanager进行此操作。Android按钮不动?
TransitionManager.beginDelayedTransition(mylayout);
然后我想,如果我能做到从顶端左侧还右下角的动画(这不包括在本教程中,我只尝试了这一点纯粹的好奇心)。我创建称为
moveButtonBack();
X变量得到由每一个或moveButton moveButtonBack()被调用时递增的另一种方法,和moveButton被调用,如果x是如果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);
}
}
同样,我会很感激的答案,因为我一直在这个问题上现在几个小时!
在你的onTouch
方法检查是否触摸或触摸。只为这些事件之一写下移动按钮逻辑。
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Code for Move button/Move button back
}
更多细节对于不改变问题的按钮大小: 移动h
和w
到活动的onCreate
方法可以帮助计算。
哇!你真了不起。谢谢你解决我一直被困住的问题**我已经标记你为最佳答案并且很喜欢!我只有一个问题,如果将像素更改为dp,此代码是否会更好地工作?我会怎么做?谢谢 –
@Rich不客气。 [This](http://stackoverflow.com/a/9563438/1389997)可能会有所帮助。 – ntsh
您是否使用'x'来维持当前位置?另外onTouchListener被多次调用 - 触摸并修改。因此'x'在奇数和偶数之间交替。 – ntsh
@ntsh是的,我使用x来维持当前位置。基本上,每当按钮向下移动并放大时,x变为奇数,并且每次它向上移动并收缩时,x变为偶数。另外,我应该做些什么来使onTouchListener只被调用一次,因为我怀疑这可能是问题所在。我很抱歉迟到的回应,我真的很感激你的时间。 –