gpt4 book ai didi

java - 相同的代码在不同的 Activity 中不起作用

转载 作者:行者123 更新时间:2023-11-30 02:40:34 25 4
gpt4 key购买 nike

我有这段代码,应该在我的一个 Activity 中捕捉滑动事件:

private float x1,x2;
static final int MIN_DISTANCE = 150;
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
float deltaX = x2 - x1;

if (Math.abs(deltaX) > MIN_DISTANCE)
{
// Left to Right swipe action
if (x2 > x1)
{
Toast.makeText(this, "Left to Right swipe [Previous]", Toast.LENGTH_SHORT).show ();
this.overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);

}

// Right to left swipe action
else
{
Toast.makeText(this, "Right to Left swipe [Next]", Toast.LENGTH_SHORT).show ();
}

}
else
{
// TO DO
}
break;
}
return super.onTouchEvent(event);
}

现在我只是将相同的工作代码移动到另一个 Activity 中,当我向左或向右滑动时它没有显示 toast 消息。是什么导致了这个问题?我知道我错过了一些非常小的东西,但我现在无法发现它。

编辑非工作 Activity :

public class BulgarianSayings extends Activity {

private float x1,x2;
static final int MIN_DISTANCE = 150;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bulgarian_sayings);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bulgarian_sayings, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bulgarian_sayings, container, false);
return rootView;
}
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
float deltaX = x2 - x1;

if (Math.abs(deltaX) > MIN_DISTANCE)
{
// Left to Right swipe action
if (x2 > x1)
{
Toast.makeText(this, "Left to Right swipe [Previous]", Toast.LENGTH_SHORT).show ();
this.overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);

}

// Right to left swipe action
else
{
Toast.makeText(this, "Right to Left swipe [Next]", Toast.LENGTH_SHORT).show ();
}

}
else
{
// consider as something else - a screen tap for example
}
break;
}
return super.onTouchEvent(event);
}
}

最佳答案

您应该在 View 上附加 onTouchListener 以使其工作。

例如,如果你想在整个 View 上实现这个,你可以这样做:

// First make an id for your View (LinearLayout, RelativeLayout, etc.) 
// and then decaler it as a view.
View view = findViewById(R.id.rellayout);

// then append onTouchListener on it
view.setOnTouchListener(Your OnTouchListener Class or Method);

另一种方法是在 Activity 顶部像这样工作:

public class BulgarianSayings extends Activity implements OnTouchListener {

//Your Other parts

关于java - 相同的代码在不同的 Activity 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25821020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com