首页 > 代码库 > Dialog列表以及实现记住选择

Dialog列表以及实现记住选择

这里简单的把代码贴上来吧,也没啥可讲的,用到SharedPreferences,都看得懂,只是有个要注意的地方———看绿色注释的地方。

 1 public class MainActivity extends Activity { 2     private TextView tv1; 3     private TextView tv2; 4     private TextView tv3; 5      6     @Override 7     protected void onCreate(Bundle savedInstanceState) { 8         super.onCreate(savedInstanceState); 9         setContentView(R.layout.activity_main);10         tv1 = (TextView)findViewById(R.id.tv1);11         tv2 = (TextView)findViewById(R.id.tv2);12         tv3 = (TextView)findViewById(R.id.tv3);13         DiaologOnItemClickListener Listener = new DiaologOnItemClickListener();14         tv1.setOnClickListener(Listener);15         tv2.setOnClickListener(Listener);16     }17     private class DiaologOnItemClickListener implements OnClickListener{18         private int weizhi;//weizhi代表着上次被选中的位置,打开应用后先去SharedPreferences里面取值,如果没有就以0为默认值,看25行19         private SharedPreferences pref;20         private SharedPreferences.Editor editor;21         @Override22         public void onClick(View v) {23             // TODO Auto-generated method stub24             pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);//24和25行不能写在onCreate方法里面,只能写在点击事件里面,因为25             weizhi = pref.getInt("weizhi",0);//...每生成一次点击事件,程序就会去SharedPreferences取值,以此来达到记住的效果26             switch(v.getId()){27             case R.id.tv1:28                 final String[] lang1 = {"南京","上海","北京"};29                 new AlertDialog.Builder(MainActivity.this).setTitle("城市列表选择:").setSingleChoiceItems(lang1,weizhi,new30                         DialogInterface.OnClickListener() {31                             @Override32                             public void onClick(DialogInterface dialog,33                                     int which) {34                                 // TODO Auto-generated method stub35                                 //dialog.dismiss();36                                 editor = pref.edit();37                                 editor.putInt("weizhi", which);38                                 editor.commit();39                                 tv3.setText("您选择的城市是:"+lang1[which]);40                             }41                 }).setPositiveButton("确定", new DialogInterface.OnClickListener(){42                         @Override43                         public void onClick(DialogInterface dialog, int which) {44                             // TODO Auto-generated method stub45                         46                         }47                 }).show();48                 break;49             case R.id.tv2:50                 String[] lang2 = {"浦东新区","闵行区","南汇区"};51                 new AlertDialog.Builder(MainActivity.this).setTitle("城市列表选择:").setSingleChoiceItems(lang2,0,new52                         DialogInterface.OnClickListener() {53                             @Override54                             public void onClick(DialogInterface dialog,55                                     int which) {56                                 // TODO Auto-generated method stub57                                 dialog.dismiss();58                             }59                 }).setPositiveButton("确定", new DialogInterface.OnClickListener(){60                         @Override61                         public void onClick(DialogInterface dialog, int which) {62                             // TODO Auto-generated method stub63                                 64                         }65                 }).show();66                 break;67             default:68                 break;69             }70         }71     }72 }

 

Dialog列表以及实现记住选择