Android中KeyEvent.KEYCODE_BACK的疑问
我有 3 项活动。 第一个活动有一个“向右走”图像按钮。 (转到第二个活动) 第二个活动有“向右走”和“向左走”图像按钮。 (返回第一个活动或移至第三个活动)。第三个活动有一个“向左”按钮可以移至第二个活动。 我已经为后退按键创建了事件。
问题是, 您从第 1 个活动导航到第 3 个活动,然后返回并导航到第 3 个活动。 第一个活动---点击右转---->第二个活动---点击右转----->第三个活动---点击向左---->第二个活动----点击向左---->第一个活动----点击向右---->第二个活动---点击右转----->第三次活动。
现在,如果我在第三个活动中按后退键,它就会移至第二个活动。在这里按下返回键,它会移至第一。但在这里,如果我按返回键,它会再次转到第一个活动。只有在第一个活动中按下第二个后退键才会退出应用程序。另一个问题是如何使图像按钮仅在我按下时才可见?
我这样说有道理吗?
FirstActivity.java
package com.stylingandroid.Animation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class FirstActivity extends Activity
{
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.first );
((ImageButton)findViewById( R.id.NextButton )).setOnClickListener( new OnClickListener()
{
@Override
public void onClick( View v )
{
startActivity( new Intent( FirstActivity.this, SecondActivity.class ) );
overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
}
});
}
@Override
public boolean onKeyUp( int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
finish();
return true;
}
return super.onKeyUp( keyCode, event );
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.next:
startActivity( new Intent( FirstActivity.this, SecondActivity.class ) );
overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
return true;
case R.id.exit:
finish();
default:
return super.onOptionsItemSelected(item);
}
}
}
SecondActivity.java
package com.stylingandroid.Animation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class SecondActivity extends Activity
{
private ImageButton nextbutton;
private ImageButton backbutton;
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.second );
nextbutton = (ImageButton) findViewById(R.id.NextButton);
backbutton = (ImageButton) findViewById(R.id.BackButton);
nextbutton.setOnClickListener( new OnClickListener()
{
@Override
public void onClick( View v )
{
startActivity( new Intent( SecondActivity.this, ThirdActivity.class ) );
overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
}
});
backbutton.setOnClickListener( new OnClickListener()
{
@Override
public void onClick( View v )
{
finish();
startActivity( new Intent( SecondActivity.this, FirstActivity.class ) );
overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
}
});
}
@Override
public boolean onKeyUp( int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
finish();
overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
return true;
}
return super.onKeyUp( keyCode, event );
}
}
ThirdActivity.java
package com.stylingandroid.Animation;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
public class ThirdActivity extends Activity
{
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.third );
((ImageButton)findViewById( R.id.BackButton )).setOnClickListener( new OnClickListener()
{
@Override
public void onClick( View v )
{
goBack();
}
});
}
private void goBack()
{
finish();
overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
}
@Override
public boolean onKeyUp( int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
goBack();
return true;
}
return super.onKeyUp( keyCode, event );
}
}
I have 3 activities.
1st activity has a 'go right' image button. (to move to 2nd activity)
2nd activity has both 'go right' and 'go left' image buttons. (to go back to 1st activity or move to 3rd activity). 3rd activity has a 'go left' button to move to 2nd activity.
And I have created events for back key press.
Problem is,
You navigate from 1st to 3rd, come back and then navigate to 3rd activity.
1st activity---clicking go right----> 2nd activity---clicking go right-----> 3rd activity---clicking go left----> 2nd activity----clicking go left----> 1st activity----clicking go right----> 2nd activity---clicking go right-----> 3rd activity.
Now if I press back key on the 3rd activity, it moves to 2nd. and on pressing back key here, it moves to 1st. But here if I press back key, it again goes to 1st activity. Only the 2nd back key press on 1st activity exits the application. An additional question is how do I make the Imagebutton visible only If I press there?
Do I make any sense?
FirstActivity.java
package com.stylingandroid.Animation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class FirstActivity extends Activity
{
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.first );
((ImageButton)findViewById( R.id.NextButton )).setOnClickListener( new OnClickListener()
{
@Override
public void onClick( View v )
{
startActivity( new Intent( FirstActivity.this, SecondActivity.class ) );
overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
}
});
}
@Override
public boolean onKeyUp( int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
finish();
return true;
}
return super.onKeyUp( keyCode, event );
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.next:
startActivity( new Intent( FirstActivity.this, SecondActivity.class ) );
overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
return true;
case R.id.exit:
finish();
default:
return super.onOptionsItemSelected(item);
}
}
}
SecondActivity.java
package com.stylingandroid.Animation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class SecondActivity extends Activity
{
private ImageButton nextbutton;
private ImageButton backbutton;
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.second );
nextbutton = (ImageButton) findViewById(R.id.NextButton);
backbutton = (ImageButton) findViewById(R.id.BackButton);
nextbutton.setOnClickListener( new OnClickListener()
{
@Override
public void onClick( View v )
{
startActivity( new Intent( SecondActivity.this, ThirdActivity.class ) );
overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
}
});
backbutton.setOnClickListener( new OnClickListener()
{
@Override
public void onClick( View v )
{
finish();
startActivity( new Intent( SecondActivity.this, FirstActivity.class ) );
overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
}
});
}
@Override
public boolean onKeyUp( int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
finish();
overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
return true;
}
return super.onKeyUp( keyCode, event );
}
}
ThirdActivity.java
package com.stylingandroid.Animation;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
public class ThirdActivity extends Activity
{
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.third );
((ImageButton)findViewById( R.id.BackButton )).setOnClickListener( new OnClickListener()
{
@Override
public void onClick( View v )
{
goBack();
}
});
}
private void goBack()
{
finish();
overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
}
@Override
public boolean onKeyUp( int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
goBack();
return true;
}
return super.onKeyUp( keyCode, event );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一个错误:
您正在
finish()
ing 和startActivity()
ing FirstActivity,导致它两次位于 Activity 堆栈上。顺便说一句,您想在这里实现什么目标?
系统将后退按钮附加到
finish()
。您不需要在 onKey 级别处理它。There's a bug here:
You are
finish()
ing andstartActivity()
ing FirstActivity as a result of which it is sitting on the Activity stack twice.On a side note, what are you trying to achieve here?
The system attaches the back button to
finish()
. You don't need to handle it at the onKey level.