2016-04-25 19 views
0

問題があります。 自分のレイアウトでDialogを作成するメソッドを作った。私のEdiTextから値(文字列)を渡して、アクティビティ内の変数にどのように渡すかわかりません。 コメントでは、私がこれをどのように解決しようとしていたかを見ることができます。ダイアログからアクティビティにデータを渡す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

私が正しくあなたの質問を理解している場合は、次に

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

を使用する必要があるので、 あなたのエディットテキストはdialog_ipであります編集テキストのテキストを

とすることができます
String text= ipValueConnection.getText().toString; 

あなたのアクティビティでその変数を使用してください。

+0

java.lang.NullPointerException:NULLオブジェクト参照で仮想メソッド '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(); 
} 

がエラー

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

//そして、あなたのダイアログclasssや方法

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

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

      onClickInterface.onClick(); 


       dialog.dismiss(); 

      } 
     }); 
} 
1

に)それはあなたのアクティビティのonCreate(でそれをインスタンスあなたを呼び出すインターフェイスを作成します。現在のレイアウトファイルでeditTextへの参照が見つからないということを意味しています。アクティビティビューの代わりにカスタムダイアログビューでEditTextを見つけました。

ので、代わりの:

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

使用:

final EditText ipValueConnection =(EditText)dialog.findViewById(R.id.ipValueConnection); 
関連する問題