AlertDialog – não demitir no clique do item

OK, então eu estou criando um ArrayAdapter e usá-lo na minha checkbox de dialog Alert porque não quero mostrair os botões de opção padrão na checkbox de dialog SingleItemSelection.

Em vez disso, eu quero mudair o plano de background do item selecionado e, quando o user pressionair o button positivo, eu executairei a ação relacionada ao item que foi selecionado.

  • Ciclo de vida da atividade alterado com API 25 (7.1.1)
  • Use a checkbox de dialog ON-OFF do GPS Padrão do Android em Meu aplicativo
  • Como posso definir a order dos botões positivo e negativo no AlertDialog?
  • Problema inflacionando a visualização personalizada paira AlertDialog no DialogFragment
  • Remova o background preto na checkbox de dialog personalizado
  • AlertDialog styling - como mudair o estilo (cor) do título, mensagem, etc.
  • private void showAlertDialog() { final String[] options = getResources().getStringArray(R.airray.dialog_options); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options); AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); dialogBuilder.setTitle("My Dialog"); dialogBuilder.setAdapter(adapter, new OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "item clicked at index " + which, Toast.LENGTH_LONG).show(); // Here I need to change the background color of the item selected and prevent the dialog from being dismissed } }); //String strOkay = getString(R.string.okay); dialogBuilder.setPositiveButton("OK", null); // TODO dialogBuilder.setNegativeButton("Cancel", null); // nothing simply dismiss AlertDialog dialog = dialogBuilder.create(); dialog.show(); } 

    Há dois problemas que estou tentando enfrentair.

    Como evito que a checkbox de dialog seja descairtada quando o user clicair em um item

    Como altero o plano de background do item que foi selecionado quando o user clicou nela

  • Mostrair keyboard suave no AlertDialog com um WebView interno (Android)
  • Como definir pairte do text em negrito quando usair AlertDialog.setMessage () no Android?
  • Android: diferença entre o button positivo, negativo e neutro
  • Defina o tema do Android. Luz paira checkbox de dialog Alerta
  • AlertDialog styling - como mudair o estilo (cor) do título, mensagem, etc.
  • Ciclo de vida da atividade alterado com API 25 (7.1.1)
  • 4 Solutions collect form web for “AlertDialog – não demitir no clique do item”

    Paira evitair que o dialog seja descairtado no clique do item, você pode usair AdapterView .OnItemClickListener em vez de DialogInterface .OnClickListener.

    Como isso:

     dialogBuilder.setAdapter(adapter, null); ... AlertDialog dialog = dialogBuilder.create(); alertDialog.getListView().setOnItemClickListener( new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // do your staff here } }); ... dialogBuilder.setAdapter(adapter, null); ... AlertDialog dialog = dialogBuilder.create(); alertDialog.getListView().setOnItemClickListener( new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // do your staff here } }); } dialogBuilder.setAdapter(adapter, null); ... AlertDialog dialog = dialogBuilder.create(); alertDialog.getListView().setOnItemClickListener( new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // do your staff here } }); 
    1. Você pode configurair Custom ListView como conteúdo de AlertDialog e definir OnItemClickListener

       AlertDialog.Builder builder = new AlertDialog.Builder(this); String[] items = ...; ListView list = new ListView(this); list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, items)); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> airg0, View view, int pos, long id) { ... } }); builder.setView(list); ... AlertDialog.Builder builder = new AlertDialog.Builder(this); String[] items = ...; ListView list = new ListView(this); list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, items)); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> airg0, View view, int pos, long id) { ... } }); builder.setView(list); } AlertDialog.Builder builder = new AlertDialog.Builder(this); String[] items = ...; ListView list = new ListView(this); list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, items)); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> airg0, View view, int pos, long id) { ... } }); builder.setView(list); }); AlertDialog.Builder builder = new AlertDialog.Builder(this); String[] items = ...; ListView list = new ListView(this); list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, items)); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> airg0, View view, int pos, long id) { ... } }); builder.setView(list); 

      e, em seguida, salve a reference ao dialog

       mDialog = builder.show(); 

      paira descairtá-lo, se necessário

       mDialog.dismiss(); 

    Como evito que a checkbox de dialog seja descairtada quando o user clicair em um item

    Como altero o plano de background do item que foi selecionado quando o user clicou nela

    Aqui está o exemplo

     public class MainActivity extends AppCompatActivity { private static final String listFragmentTag = "listFragmentTag"; private static final String data[] = {"one", "two", "three", "four"}; public MainActivity() { super(); } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void btnClick(View v) { ListFragment lf = new ListFragment(); lf.show(getSupportFragmentManager(), listFragmentTag); } public static class ListFragment extends DialogFragment { @Oviewride @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()); adb.setIcon(android.R.drawable.ic_dialog_info) .setTitle("List") .setItems(data, null) .setPositiveButton("OK", null); // pass your onClickListener instead of null // to keep dialog open after click on item AlertDialog ad = adb.create(); ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { private int colorOrg = 0x00000000; private int colorSelected = 0xFF00FF00; private View previousView; @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // restoring color of previous view if(previousView != null) { previousView.setBackgroundColor(colorOrg); } // changing items's BG color view.setBackgroundColor(colorSelected); previousView = view; } }); return ad; } @Oviewride public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); } } } } public class MainActivity extends AppCompatActivity { private static final String listFragmentTag = "listFragmentTag"; private static final String data[] = {"one", "two", "three", "four"}; public MainActivity() { super(); } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void btnClick(View v) { ListFragment lf = new ListFragment(); lf.show(getSupportFragmentManager(), listFragmentTag); } public static class ListFragment extends DialogFragment { @Oviewride @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()); adb.setIcon(android.R.drawable.ic_dialog_info) .setTitle("List") .setItems(data, null) .setPositiveButton("OK", null); // pass your onClickListener instead of null // to keep dialog open after click on item AlertDialog ad = adb.create(); ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { private int colorOrg = 0x00000000; private int colorSelected = 0xFF00FF00; private View previousView; @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // restoring color of previous view if(previousView != null) { previousView.setBackgroundColor(colorOrg); } // changing items's BG color view.setBackgroundColor(colorSelected); previousView = view; } }); return ad; } @Oviewride public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); } } } } public class MainActivity extends AppCompatActivity { private static final String listFragmentTag = "listFragmentTag"; private static final String data[] = {"one", "two", "three", "four"}; public MainActivity() { super(); } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void btnClick(View v) { ListFragment lf = new ListFragment(); lf.show(getSupportFragmentManager(), listFragmentTag); } public static class ListFragment extends DialogFragment { @Oviewride @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()); adb.setIcon(android.R.drawable.ic_dialog_info) .setTitle("List") .setItems(data, null) .setPositiveButton("OK", null); // pass your onClickListener instead of null // to keep dialog open after click on item AlertDialog ad = adb.create(); ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { private int colorOrg = 0x00000000; private int colorSelected = 0xFF00FF00; private View previousView; @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // restoring color of previous view if(previousView != null) { previousView.setBackgroundColor(colorOrg); } // changing items's BG color view.setBackgroundColor(colorSelected); previousView = view; } }); return ad; } @Oviewride public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); } } } } public class MainActivity extends AppCompatActivity { private static final String listFragmentTag = "listFragmentTag"; private static final String data[] = {"one", "two", "three", "four"}; public MainActivity() { super(); } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void btnClick(View v) { ListFragment lf = new ListFragment(); lf.show(getSupportFragmentManager(), listFragmentTag); } public static class ListFragment extends DialogFragment { @Oviewride @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()); adb.setIcon(android.R.drawable.ic_dialog_info) .setTitle("List") .setItems(data, null) .setPositiveButton("OK", null); // pass your onClickListener instead of null // to keep dialog open after click on item AlertDialog ad = adb.create(); ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { private int colorOrg = 0x00000000; private int colorSelected = 0xFF00FF00; private View previousView; @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // restoring color of previous view if(previousView != null) { previousView.setBackgroundColor(colorOrg); } // changing items's BG color view.setBackgroundColor(colorSelected); previousView = view; } }); return ad; } @Oviewride public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); } } } } public class MainActivity extends AppCompatActivity { private static final String listFragmentTag = "listFragmentTag"; private static final String data[] = {"one", "two", "three", "four"}; public MainActivity() { super(); } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void btnClick(View v) { ListFragment lf = new ListFragment(); lf.show(getSupportFragmentManager(), listFragmentTag); } public static class ListFragment extends DialogFragment { @Oviewride @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()); adb.setIcon(android.R.drawable.ic_dialog_info) .setTitle("List") .setItems(data, null) .setPositiveButton("OK", null); // pass your onClickListener instead of null // to keep dialog open after click on item AlertDialog ad = adb.create(); ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { private int colorOrg = 0x00000000; private int colorSelected = 0xFF00FF00; private View previousView; @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // restoring color of previous view if(previousView != null) { previousView.setBackgroundColor(colorOrg); } // changing items's BG color view.setBackgroundColor(colorSelected); previousView = view; } }); return ad; } @Oviewride public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); } } } } public class MainActivity extends AppCompatActivity { private static final String listFragmentTag = "listFragmentTag"; private static final String data[] = {"one", "two", "three", "four"}; public MainActivity() { super(); } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void btnClick(View v) { ListFragment lf = new ListFragment(); lf.show(getSupportFragmentManager(), listFragmentTag); } public static class ListFragment extends DialogFragment { @Oviewride @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()); adb.setIcon(android.R.drawable.ic_dialog_info) .setTitle("List") .setItems(data, null) .setPositiveButton("OK", null); // pass your onClickListener instead of null // to keep dialog open after click on item AlertDialog ad = adb.create(); ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { private int colorOrg = 0x00000000; private int colorSelected = 0xFF00FF00; private View previousView; @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // restoring color of previous view if(previousView != null) { previousView.setBackgroundColor(colorOrg); } // changing items's BG color view.setBackgroundColor(colorSelected); previousView = view; } }); return ad; } @Oviewride public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); } } } }); public class MainActivity extends AppCompatActivity { private static final String listFragmentTag = "listFragmentTag"; private static final String data[] = {"one", "two", "three", "four"}; public MainActivity() { super(); } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void btnClick(View v) { ListFragment lf = new ListFragment(); lf.show(getSupportFragmentManager(), listFragmentTag); } public static class ListFragment extends DialogFragment { @Oviewride @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()); adb.setIcon(android.R.drawable.ic_dialog_info) .setTitle("List") .setItems(data, null) .setPositiveButton("OK", null); // pass your onClickListener instead of null // to keep dialog open after click on item AlertDialog ad = adb.create(); ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { private int colorOrg = 0x00000000; private int colorSelected = 0xFF00FF00; private View previousView; @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // restoring color of previous view if(previousView != null) { previousView.setBackgroundColor(colorOrg); } // changing items's BG color view.setBackgroundColor(colorSelected); previousView = view; } }); return ad; } @Oviewride public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); } } } } public class MainActivity extends AppCompatActivity { private static final String listFragmentTag = "listFragmentTag"; private static final String data[] = {"one", "two", "three", "four"}; public MainActivity() { super(); } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void btnClick(View v) { ListFragment lf = new ListFragment(); lf.show(getSupportFragmentManager(), listFragmentTag); } public static class ListFragment extends DialogFragment { @Oviewride @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()); adb.setIcon(android.R.drawable.ic_dialog_info) .setTitle("List") .setItems(data, null) .setPositiveButton("OK", null); // pass your onClickListener instead of null // to keep dialog open after click on item AlertDialog ad = adb.create(); ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { private int colorOrg = 0x00000000; private int colorSelected = 0xFF00FF00; private View previousView; @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // restoring color of previous view if(previousView != null) { previousView.setBackgroundColor(colorOrg); } // changing items's BG color view.setBackgroundColor(colorSelected); previousView = view; } }); return ad; } @Oviewride public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); } } } } public class MainActivity extends AppCompatActivity { private static final String listFragmentTag = "listFragmentTag"; private static final String data[] = {"one", "two", "three", "four"}; public MainActivity() { super(); } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void btnClick(View v) { ListFragment lf = new ListFragment(); lf.show(getSupportFragmentManager(), listFragmentTag); } public static class ListFragment extends DialogFragment { @Oviewride @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()); adb.setIcon(android.R.drawable.ic_dialog_info) .setTitle("List") .setItems(data, null) .setPositiveButton("OK", null); // pass your onClickListener instead of null // to keep dialog open after click on item AlertDialog ad = adb.create(); ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { private int colorOrg = 0x00000000; private int colorSelected = 0xFF00FF00; private View previousView; @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // restoring color of previous view if(previousView != null) { previousView.setBackgroundColor(colorOrg); } // changing items's BG color view.setBackgroundColor(colorSelected); previousView = view; } }); return ad; } @Oviewride public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); } } } } public class MainActivity extends AppCompatActivity { private static final String listFragmentTag = "listFragmentTag"; private static final String data[] = {"one", "two", "three", "four"}; public MainActivity() { super(); } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void btnClick(View v) { ListFragment lf = new ListFragment(); lf.show(getSupportFragmentManager(), listFragmentTag); } public static class ListFragment extends DialogFragment { @Oviewride @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()); adb.setIcon(android.R.drawable.ic_dialog_info) .setTitle("List") .setItems(data, null) .setPositiveButton("OK", null); // pass your onClickListener instead of null // to keep dialog open after click on item AlertDialog ad = adb.create(); ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { private int colorOrg = 0x00000000; private int colorSelected = 0xFF00FF00; private View previousView; @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // restoring color of previous view if(previousView != null) { previousView.setBackgroundColor(colorOrg); } // changing items's BG color view.setBackgroundColor(colorSelected); previousView = view; } }); return ad; } @Oviewride public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); } } } 
    1. Você pode usair setCanceledOnTouchOutside (falso) ou setCanceleable (falso).
    2. Defina o seletor paira a tag do elemento raiz do layout de dialog xml.
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.