ListPreferences without any radio buttons?

2019-02-28 02:23发布

I want to create a ListPreference in my PreferenceActivity.

When a ListPreference is clicked, I get a dialog box with a listview. Each row in list view has a text field and a radio button.

I do not want this radio button and also on clicking list item, I want to fire an intent that opens browser? Any idea how to go about it?

If i extend DialogPreference then how to handle onClicks? Like onListClickListener will work?

OR

If i extend ListPreference what are the functions i need to override?

1条回答
来,给爷笑一个
2楼-- · 2019-02-28 02:41

This is possible when you are customizing preferences.When you are using only Preference ,it works like a button.And later you have to implement whatever you want.The Following example simply shows as your requirement.When you click preference,it shows list dialog without radio buttons .But i am not implemented to store the data in Shared preferences.If you want to do that,you have to implement your own.I just post some code here.

prefereces=findPreference("intent");
       // prefereces.setIntent(new Intent(Intent.ACTION_VIEW,Uri.parse("https://market.android.com/")));
  // prefereces.setIntent(new Intent(getApplicationContext(), DynamicPreference.class));

       prefereces.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            // TODO Auto-generated method stub

            createListPreferenceDialog();
            return true;
        }


    });


    }
    private void createListPreferenceDialog()
    {

        Dialog dialog;
        final CharSequence str[]={"Android","Black Berry","Symbian"};
        AlertDialog.Builder b=new AlertDialog.Builder(PreferenceActivities1Activity.this);
        b.setTitle("Mobile OS");

        b.setItems(str, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int position)
            {

                showToast("I am Clicked "+str[position]);
//              switch (position) 
//              {
//              case 0:
//                  showToast("I am Clicked "+str[position]);
//                  break;
//
//              default:
//                  break;
//              }

            }
        });

        dialog=b.create();
        dialog.show();

    }
    public void showToast(String msg)
    {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }
查看更多
登录 后发表回答