2016-04-25 41 views
0

我有問題。 我做了用我自己的佈局創建Dialog的方法。我不知道如何從我的EdiText傳遞值(字符串)並將其作爲我Activity中的任何變量。 在評論中,你可以看到我是如何解決這個問題的。將數據從對話框傳遞到活動Android

Java方法

public void makeDialog(){ 
     // custom dialog 
     final Dialog dialog = new Dialog(context); 
     dialog.setContentView(R.layout.dialog_ip); 
     dialog.setTitle("IP connection"); 

// Todo passing value from dialog to activity 
//  final EditText ipValueConnection = (EditText)findViewById(R.id.ipValueConnection); 
//  ipValueConnection.setOnClickListener(this); 
//  EditText portValueConnection = (EditText)findViewById(R.id.portValueConnection); 
//  Toast.makeText(context, ipValueConnection.getText().toString(), Toast.LENGTH_LONG).show(); 

     Button dialogButtonLogin = (Button) dialog.findViewById(R.id.dialogButtonLogin); 
     // if button is clicked, close the custom dialog 
     dialogButtonLogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


       tryToConnect(); 
       dialog.dismiss(); 

      } 
     }); 




     // set the custom dialog components - text, image and button 
//  TextView text = (TextView) dialog.findViewById(R.id.IP); 

     dialog.show(); 

    } 

XML佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <ImageView 
     android:src="@drawable/antena" 
     android:layout_width="220dp" 
     android:layout_height="120dp" 
     android:scaleType="centerInside" 
     android:background="#FFFFBB33" 
     android:contentDescription="@string/app_name" 
     android:adjustViewBounds="true" 

     /> 
    <EditText 
     android:id="@+id/ipValueConnection" 
     android:inputType="textEmailAddress" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="16dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginBottom="4dp" 
     android:hint="IP" /> 
    <EditText 
     android:id="@+id/portValueConnection" 
     android:inputType="textPassword" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="4dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginBottom="16dp" 
     android:fontFamily="sans-serif" 
     android:hint="PORT"/> 
    <Button 
     android:id="@+id/dialogButtonLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Login" 
     android:layout_marginTop="5dp" 
     /> 
</LinearLayout> 
+0

可以做出重要的一個HashMap - 值對並寄回。 –

+0

但如何將其發回? –

+2

你可以製作一個'interface'和'onClick()'按鈕,你可以傳遞數據。 –

回答

0

如果我理解正確你的問題, 您編輯文本在dialog_ip,所以你需要使用

final EditText ipValueConnection = (EditText) dialog.findViewById(R.id.ipValueConnection); 

然後你可以從編輯文本中獲得文本

String text= ipValueConnection.getText().toString; 

並使用您的活動這個變量。

+0

顯示java.lang.NullPointerException:嘗試在空對象引用 我有這種錯誤的調用虛擬方法android.text.Editable android.widget.EditText.getText()「如果我這樣做 –

+0

錯誤說,編輯你想要的文本不在視圖中..你可以編輯你的評論部分,如上面的答案,然後嘗試運行它.. – Bee

+0

我試過你的代碼,並且如果我使用'final EditText ipValueConnection = (的EditText)findViewById(R.id.ipValueConnection);'這是您的代碼..嘗試編輯它作爲'最終的EditText ipValueConnection =(EditText上)dialog.findViewById(R.id.ipValueConnection);' – Bee

0

創建界面

public interface OnClickInterface { 

    public void onClick(); 
} 

調用它實例化它在你活動的onCreate()

OnClickInterface onClickInterface = new OnClickInterface() { 
      @Override 
      public void onClick() { 
       //Call Method from here 
       requiredMethod(); 
      } 
     }; 

//並在對話框班組長或方法

public void makeDialog(OnClickInterface onClickInterface){ 
//Your code 

dialogButtonLogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

      onClickInterface.onClick(); 


       dialog.dismiss(); 

      } 
     }); 
} 
1

錯誤你越來越意味着無法在當前佈局文件中找到對editText的引用。您可以在自定義對話框視圖中找到EditText,而不是活動視圖。

所以不是:

final EditText ipValueConnection =(EditText)findViewById(R.id.ipValueConnection); 

使用:

final EditText ipValueConnection =(EditText)dialog.findViewById(R.id.ipValueConnection); 
相關問題