0

我遇到的问题是从我的选项菜单对话框中捕获数据。下面的示例有一个带有四个对话框布局的选项菜单,该对话框捕获了我需要返回给主机活动的字符串。有人可以解释为什么下面的代码不起作用。这是我的代码示例:

class ClientConfigActivity extends Activity with TypedActivity {

// Called when the activity is first created                        
override def onCreate(savedInstanceState: Bundle) {                 
  super.onCreate(savedInstanceState)                                
  setContentView(R.layout.clientconfig)                             

  // Dialog Test                                                    
  Log.d("Option", getIntent.getExtras.get("localIP").toString)      
  Log.d("Option", getIntent.getExtras.get("remoteIP").toString)     
}                                                                   

// Relieves results from clientConfig option menu                                     
override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {      
  super.onActivityResult(requestCode, resultCode, data)                             

  requestCode match {                                                                 
    case 0 => Log.d("Option", "Option 1 got here")                                    

      //local_IPAddress.setText("hello")                                              

    case 1 => Log.d("Option", "Option 2 got here")                                    
  }                                                                                   
}         

// Called when an options menu is created.               
override def onCreateOptionsMenu(menu: Menu) = {         
  // Inflates XML file into something that can be used.  
  val inflater = getMenuInflater                         
  inflater.inflate(R.menu.clientconfigmenu, menu)        

  super.onCreateOptionsMenu(menu)                        
}      

// Test to see which option was clicked.                                                                                           
override def onOptionsItemSelected(item: MenuItem) = {                                                                             
  // Check for clientLocation_option                                                                                               
  item.getItemId match {                                                                                                           

    case R.id.option_LocalConfig => {                                                                                              
      // Set reference to dialog_localconfig_layout.xml                                                                            
      val dialog = new Dialog(this)                                                                                                
      dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON)                                                                        
      dialog.setContentView(R.layout.dialog_localconfig_layout)                                                                    
      dialog.setTitle("Local Configuration")                                                                                       

      // dialog_remoteconfig_layout.xml References                                                                                 
      val local_IP = dialog.findViewById(R.id.setLocalIPaddress).asInstanceOf[EditText]                                            
      val local_PortNumber = dialog.findViewById(R.id.setLocalPortNum).asInstanceOf[EditText]                                      
      val local_Done_Button = dialog.findViewById(R.id.local_DoneButton).asInstanceOf[Button]                                      

      // RemoteConfig Done Button Listener                                                                                         
      dialog.show()                                                                                                                
      dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.ic_menu_myplaces)                             

      // LocalConfig Done Button Listener                                                                                          
      local_Done_Button.setOnClickListener(new OnClickListener {                                                                   
        override def onClick(view: View) {                                                                                         
          // Start the ClientConfig Activity w/explicit intent                                                                     
          val intent = new Intent(dialog.getContext, classOf[ClientConfigActivity])                                                
          intent.putExtra("localIP", local_IP.getText.toString)                                                                    
          intent.putExtra("localPort", local_PortNumber.getText.toString)                                                          
          startActivityForResult(intent, 0)                                                                                        
        }                                                                                                                          
      })                                                                                                                           
    }                                                                                                                              

    case R.id.option_RemoteConfig => {                                                                                             
      // Set reference to dialog_remoteconfig_layout.xml                                                                           
      val dialog = new Dialog(this)                                                                                                
      dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON)                                                                        
      dialog.setContentView(R.layout.dialog_remoteconfig_layout)                                                                   
      dialog.setTitle("Remote Configuration")                                                                                      

      // dialog_remoteconfig_layout.xml References                                                                                 
      val remote_IP = dialog.findViewById(R.id.setRemoteIP).asInstanceOf[EditText]                                                 
      val remote_PortNumber = dialog.findViewById(R.id.setRemotePortNum).asInstanceOf[EditText]                                    
      val remote_Done_Button = dialog.findViewById(R.id.remote_DoneButton).asInstanceOf[Button]                                    

      //Show dialog_remoteconfig_layout Custom Layout                                                                              
      dialog.show()                                                                                                                
      dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.ic_menu_call)                                 

      // RemoteConfig Done Button Listener                                                                                         
      remote_Done_Button.setOnClickListener(new OnClickListener {                                                                  
        override def onClick(view: View) {                                                                                         
          // Start the ClientConfig Activity w/explicit intent                                                                     
          val intent = new Intent(dialog.getContext, classOf[ClientConfigActivity])                                                
          intent.putExtra("remoteIP", remote_IP.getText.toString)                                                                  
          intent.putExtra("remotePortNum", remote_PortNumber.getText.toString)                                                     
          startActivityForResult(intent, 1)                                                                                        
        }                                                                                                                          
      })                                                                                                                           
    }                                                                                                                              

  }                                                                                                                                

  super.onOptionsItemSelected(item)                                                                                                
}                                                                                                                                                                                                                                                                
4

1 回答 1

0

当您尝试显示对话框时会发生什么?

对话框的生命周期可能看起来有点奇怪。有关详细信息,请参阅http://developer.android.com/guide/topics/ui/dialogs.html

您应该调用 showDialog(yourDialogId) 来打开对话框。对话框创建在 onCreateDialog(id) 中完成。在这里,您将 id 与 yourDialogId 匹配并创建并返回对话框。

于 2012-04-30T09:01:14.103 回答