0

私が抱えている問題は、オプション メニュー ダイアログからデータをキャプチャすることです。以下の例には、ホスト アクティビティに返す必要があるストリングをダイアログでキャプチャするための 4 つのダイアログ レイアウトを含むオプション メニューがあります。以下のコードが機能しない理由を誰かが説明できますか? これが私のコードのサンプルです:

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 に答える