如何回到previous活动大约有一个按钮,点击? [英] how to return to previous activity from about with a button click?

查看:188
本文介绍了如何回到previous活动大约有一个按钮,点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着从大约对话框通过点击一个按钮,返回到主活动:

Im trying to return from the about dialog to the main activity by a button click:

public class AboutActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    SharedPreferences prefs = getSharedPreferences("com.example.tiocontas",MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = prefs.edit();
    prefsEditor.putBoolean("FirstTime", false);
    finish();
    //this.onBackPressed();
}
}

我都试过完成()和onBack pressed()没有结果,即时通讯做错了什么可能有人给我一些提示?

I've tried both finish() and onBackPressed() with no results, im doing something wrong could someone give me some hints?

推荐答案

从我可以在你的code见,你不必连接到你的的onClick()方法。你可以这样做两种方式,以XML或编程。

From what I can see in your code, you don't have a button attached to your onClick() method. You can do this two ways, in xml or programmatically.

在XML

<Button
...
android:onClick="functionName"/>

然后在你的code,定义你的功能,你在你的XML命名为

Then in your code, define your function which you named in your xml

public void functionName(View v)
{
  // some code
  finish();
}

编程,声明你的按钮

Programmatically, declare your button

Button aBtn = (Button) findViewById(R.id.button_id);
aBtn.setOnClickListener(new OnClickListener() {         
    @Override
    public void onClick(View v)
    {
      // some code
      AboutActivity.this.finish()
    }
});

如果您已连接的按钮的onClick()在某些方面没有显示,那么你可能会完成你的主要活动。在这种情况下,描述当您单击按钮,显示会发生什么你的主要活动

If you have already attached your button to the onClick() in some way not shown then you may be finishing your main activity. In which case, describe what happens when you click the button and show your Main Activity

文档的OnClickListener()

这篇关于如何回到previous活动大约有一个按钮,点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆