单击按钮时转到下一步 activity,释放按钮时转到 Return
Go to Next activity while the Button is clicked & Return when Released
我有一个 OnTouch 按钮,当单击该按钮时使用运动事件转到下一个 activity,但我希望它仅在按下按钮时停留在下一个 activity单击然后 return...如果它看起来很复杂,这里是代码-:
@Override
public boolean onTouch(View v, MotionEvent event) {
while (event.getAction() == MotionEvent.ACTION_DOWN){
Intent myIntent = new Intent(MainActivity.this, ActionActivity.class);
myIntent.putExtra("key", value); //Optional parameters
MainActivity.this.startActivity(myIntent);
}
if(event.getAction() == MotionEvent.ACTION_UP){
}
return true;
}
});
在下一个 activity 的根视图上使用相同的 onTouch 并完成活动 ACTION_UP
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
finish();
}
return true;
}
});
(更新)测试答案:
第一个Activity:
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
viewGroup.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
MainActivity.this.startActivity(myIntent);
return true;
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
SecondActivity.activity.get().finish();
return true;
}
return true;
}
}
第二个Activity:
public class SecondActivity extends AppCompatActivity {
public static WeakReference<Activity> activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
activity = new WeakReference<Activity>(this);
}
}
- 不要在 onTouch
中使用 'while'
- 使用静态 Activity 对象从第一个 activity 完成第二个 activity,
这是有效的
我有一个 OnTouch 按钮,当单击该按钮时使用运动事件转到下一个 activity,但我希望它仅在按下按钮时停留在下一个 activity单击然后 return...如果它看起来很复杂,这里是代码-:
@Override
public boolean onTouch(View v, MotionEvent event) {
while (event.getAction() == MotionEvent.ACTION_DOWN){
Intent myIntent = new Intent(MainActivity.this, ActionActivity.class);
myIntent.putExtra("key", value); //Optional parameters
MainActivity.this.startActivity(myIntent);
}
if(event.getAction() == MotionEvent.ACTION_UP){
}
return true;
}
});
在下一个 activity 的根视图上使用相同的 onTouch 并完成活动 ACTION_UP
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
finish();
}
return true;
}
});
(更新)测试答案:
第一个Activity:
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
viewGroup.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
MainActivity.this.startActivity(myIntent);
return true;
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
SecondActivity.activity.get().finish();
return true;
}
return true;
}
}
第二个Activity:
public class SecondActivity extends AppCompatActivity {
public static WeakReference<Activity> activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
activity = new WeakReference<Activity>(this);
}
}
- 不要在 onTouch 中使用 'while'
- 使用静态 Activity 对象从第一个 activity 完成第二个 activity,
这是有效的