没有动作栏的安卓后退导航



我有 3 个屏幕,屏幕 1、屏幕 2、屏幕 3。

这是我正在实现的那种后退导航,

屏幕 3 -> 屏幕 2 ->屏幕 1。

这是我下面的屏幕 1 代码

public class Screen1 extends Activity implements OnClickListener{
    Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch(v.getId())
        {
            // This is where the intent starts to display the second Screen
            case R.id.button1:
                Intent started = new Intent(Screen1.this,Screen2.class);
                startActivity(started);
                break;
        }
    }
}

屏幕 2

public class Screen2 extends Activity implements OnTouchListener{
    ListView displaylist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);
        // Please ignore this part of the code it just displays the list of items
        displaylist  = (ListView)findViewById(R.id.listView1);
        String[] listofitems = {"A","B","C","W"};
        ArrayAdapter<String> lists = new ArrayAdapter<String>(Screen2.this, android.R.layout.simple_list_item_1, listofitems);
        displaylist.setAdapter(lists);
        displaylist.setOnTouchListener(this);
    }
    // This is where the intent starts to display the Third Screen
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        Intent started2= new Intent(Screen2.this,Screen3.class);
        startActivity(started2);
        return true;
    }
}

屏幕 3

public class Screen3 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Displays some layout
    }
}

我目前的问题

我目前的问题是后退导航(当我按下后退按钮时)按以下顺序排列

屏幕3 -> 屏幕3 -> 屏幕2 ->屏幕1

我的问题

为什么当我按下后退按钮时,Screen3 再次显示?这是某种错误吗?

来自一个类似的问题,大卫·瓦瑟评论说

If your navigation is standard, then 1 starts 2 and the BACK button goes back to 1. 2 starts 3 and the BACK button goes back to 2. You don't need to do anything special to get this

测试装置

安卓模拟器

接口等级:19

注意:我不想使用操作栏执行此操作

我认为这是因为您的onTouch事件在按下和释放时被触发了两次。

试试这个,或使用其他一些事件:

public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
    Intent started2= new Intent(Screen2.this,Screen3.class);
    startActivity(started2);
    break;
case MotionEvent.ACTION_MOVE:
    // touch move code
    break;
case MotionEvent.ACTION_UP:
    // touch up code
    break;
}
return true;
}

相关内容

  • 没有找到相关文章