如何回到previous活动大约有一个按钮,点击?有一个、按钮、previous

2023-09-12 05:42:23 作者:陌上月゛今夕何夕

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

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()

 
精彩推荐
图片推荐