gpt4 book ai didi

android - 在内置后退按钮和注销按钮中实现注销消息

转载 作者:行者123 更新时间:2023-11-29 17:54:20 25 4
gpt4 key购买 nike

这是我的 MainMenu.java

public class MainMenu extends Activity{

Button userinfo,requestservice,makepayment,trackparcel,checkcard, logout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
// Show the Up button in the action bar.
setupActionBar();
userinfo = (Button) findViewById(R.id.userinfo);
requestservice = (Button) findViewById(R.id.requestservice);
makepayment = (Button) findViewById(R.id.makepayment);
trackparcel = (Button) findViewById(R.id.trackparcel);
checkcard = (Button) findViewById(R.id.checkcard);
logout = (Button) findViewById(R.id.logout);

userinfo.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainMenu.this, UserInfo.class);
startActivity(intent);
}
});
requestservice.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainMenu.this, RequestService.class);
startActivity(intent);

}
});
makepayment.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainMenu.this, Payment.class);
startActivity(intent);

}
});
trackparcel.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainMenu.this, TrackParcel.class);
startActivity(intent);
}
});
checkcard.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainMenu.this, CheckCard.class);
startActivity(intent);
}
});
logout.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainMenu.this, Login.class);
SharedPreferences userDetails =getSharedPreferences("userdetails", MODE_PRIVATE);
Editor edit = userDetails.edit();
edit.clear();
edit.putString("email", "");
edit.putString("password", "");
edit.commit();
startActivity(intent);
}
});
}

public void dispachBackKey() {
dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}


}

我想触发内置的后退按钮,我最终找到了这段代码

public void dispachBackKey() {
dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
}

您可能会注意到我也有注销按钮,我想做的是如何显示一条小消息来询问用户是否确认注销注销按钮构建- 在后退按钮中。我应该如何实现?

最佳答案

对于后退按钮,您还可以使用 public void onBackPressed()。然后,您只需在每次按下按钮后显示一个 AlertDialog。该代码如下所示:

new AlertDialog.Builder(this)
.setTitle("Logout")
.setMessage("Would you like to logout?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// logout
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// user doesn't want to logout
}
})
.show();

这将被放置在注销按钮的 onClickListener 中,还有 onBackPressed() 或您想要检测后退按钮点击的任何方式。以下是 onBackPressed() 的示例:

public void onBackPressed() {
//put the AlertDialog code here
}

关于android - 在内置后退按钮和注销按钮中实现注销消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20586238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com