大家好我在我的主要活动XML上有一个退出按钮,但我需要它,所以当用户点击它时,它会打开一个对话框以确认退出,如果是,则退出。如果没有收回他们。
这是我的代码到目前为止,没有任何该部分,但因为我不知道从哪里开始。
package com.tradingsoftwarelimited.labelprinter10;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Sets Edit and Data Button to hidden
Button hiddenButton = (Button) findViewById(R.id.editButton);
hiddenButton.setVisibility(View.INVISIBLE);
Button hiddenButton1 = (Button) findViewById(R.id.dataButton);
hiddenButton1.setVisibility(View.INVISIBLE);
//Acknolages settings button
Button showButton = (Button) findViewById(R.id.settingsButton);
//if clicked
showButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//If clicked show Edit and data Button
Button showButton = (Button) (findViewById(R.id.editButton));
showButton.setVisibility(View.VISIBLE);
Button showButton1 = (Button) (findViewById(R.id.dataButton));
showButton1.setVisibility(View.VISIBLE);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportActionBar().hide();
return true;
}
};
;
答案 0 :(得分:2)
public static <T extends Unit> List<T> getUnitsByClass(Class<T> specificClass, List<? extends Unit> units) {
return (List<T>) units.stream()
.filter(e -> e.getClass().equals(specificClass))
.collect(Collectors.toList());
}
在
中调用此方法 private void confirmDialog(Context context){
final AlertDialog alert = new AlertDialog.Builder(
new ContextThemeWrapper(context,android.R.style.Theme_Dialog))
.create();
alert.setTitle("Alert");
alert.setMessage("Do you want to exit ?");
alert.setIcon(R.drawable.warning_icon);
alert.setCancelable(false);
alert.setCanceledOnTouchOutside(false);
alert.setButton(DialogInterface.BUTTON_POSITIVE, "Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
finish();
}
});
alert.setButton(DialogInterface.BUTTON_NEGATIVE, "No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
});
alert.show();
}
您@Override
public void onBackPressed() {
super.onBackPressed();
confirmDialog(getApplicationContext());
}
的
答案 1 :(得分:0)
试试这样:
Button exitButton = (Button) findViewById(R.id.exit_button);
exitButton..setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builde = new AlertDialog.Builder(this);
builde.setMessage(
"Want Exit?")
.setPositiveButton("Yes ", dialogClickListeners)
.setNegativeButton("Cancel", dialogClickListeners).show();
}
});
DialogInterface.OnClickListener dialogClickListeners = new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
switch (which)
{
case DialogInterface.BUTTON_POSITIVE:
finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
答案 2 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Invite = (ImageButton) findViewById(R.id.imageButton5);
final DialogInterface.OnClickListener dialogClickListeners = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
Invite.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builde = new AlertDialog.Builder(MainActivity.this);
builde.setMessage(
"Want Exit?")
.setPositiveButton("Yes ", dialogClickListeners)
.setNegativeButton("Cancel", dialogClickListeners).show();
}
});
}