Menu的使用

 

  
  
  
  
  1. @Override 
  2.     public boolean onCreateOptionsMenu(Menu menu) { 
  3.         MenuInflater inflater = getMenuInflater(); 
  4.         inflater.inflate(R.menu.lost_menu, menu); //在res目录下建一menu文件夹,建一lost_menu.xml
  5.         return true
  6.     } 
  7.     @Override 
  8.     public boolean onOptionsItemSelected(MenuItem item) { 
  9.         // Handle item selection 
  10.         switch (item.getItemId()) { 
  11.             case R.id.item_change_name: 
  12.                 AlertDialog.Builder builder = new Builder(LostProtectedActivity.this); 
  13.                 builder.setTitle("请输入标题名称"); 
  14.                 final EditText et = new EditText(this); 
  15.                 builder.setView(et); 
  16.                 builder.setPositiveButton("确定"new DialogInterface.OnClickListener() { 
  17.                      
  18.                     public void onClick(DialogInterface dialog, int which) { 
  19.                         String name = et.getText().toString().trim(); 
  20.                         Editor editor = sp.edit(); 
  21.                         editor.putString("newname", name); 
  22.                         editor.commit(); 
  23.                     } 
  24.                 }); 
  25.                 builder.create().show(); 
  26.                 return true
  27.         } 
  28.         return false
  29.     } 
   
   
   
   
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android" > 
  3.     <item android:id="@+id/item_change_name" android:title="更改名称"></item> 
  4. </menu> 


然后在MainUIAdapter类的getView()方法中写入如下代码:

    
    
    
    
  1. if(position==0){ 
  2.         SharedPreferences sp  =context.getSharedPreferences("config",Context.MODE_PRIVATE); 
  3.         String name = sp.getString("newname"""); 
  4.         if(!TextUtils.isEmpty(name)){ 
  5.             tv.setText(name); 
  6.         } 
  7.     } 

 

你可能感兴趣的:(Menu实例)