2017-12-22 56 views
0

대화 상자 호스트 (MainActivity)로 다시 클릭 이벤트를 전달하는 사용자 정의 레이아웃뿐만 아니라 사용자 정의 DialogFragment 및 인터페이스를 사용하여 대화 상자를 만들었습니다. 그러나 내 문제는 대화 상자에서 EditText에 입력 된 텍스트를 가져 오려고하면 다시 빈 문자열로 나타납니다. 어떤 아이디어? TextView의 텍스트를 대화 상자에서 가져올 수 있습니다. EditText가 아닙니다.대화 상자 textview에서 텍스트를 가져올 수 있지만 edittext 대화 상자에서 텍스트를 가져올 수 없습니다.

add_class_dialog.xml :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:padding="10dp"> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center"> 
     <TextView 
      android:id="@+id/viewClassName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Class: " 
      android:textSize="25sp" 
      android:textColor="@color/textColor"/> 
     <EditText 
      android:id="@+id/editClassName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:hint="Enter class" 
      android:textSize="25sp" 
      android:textColor="@color/textColor" 
      android:layout_gravity="center"/> 
    </LinearLayout> 
</LinearLayout> 

MainActivity.java :

public class MainActivity extends FragmentActivity implements AddClassDialogFragment.AddClassDialogListener { 

TextView nameView; 
EditText editName; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    View vi = getLayoutInflater().inflate(R.layout.add_class_dialog, null); 
    nameView = vi.findViewById(R.id.viewClassName); 
    editName = vi.findViewById(R.id.editClassCredits); 

} 
public void addClass(View view) { 
    AddClassDialogFragment acdf = new AddClassDialogFragment(); 
    acdf.show(getFragmentManager(), "ac"); 
} 
@Override 
public void onDialogPositiveClick(DialogFragment dialog) { 
    Toast.makeText(this, nameView.getText().toString(), Toast.LENGTH_SHORT).show(); 
    Toast.makeText(this, editName.getText().toString(), Toast.LENGTH_SHORT).show(); 

} 
@Override 
public void onDialogNegativeClick(DialogFragment dialog) { 
    Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show(); 
} 
} 

AddClassDialogFragment.java :

public class AddClassDialogFragment extends DialogFragment { 
public interface AddClassDialogListener { 
    void onDialogPositiveClick(DialogFragment dialog); 
    void onDialogNegativeClick(DialogFragment dialog); 
} 
AddClassDialogListener mListener; 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    try { 
     mListener = (AddClassDialogListener) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); 
    } 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    final LayoutInflater inflater = getActivity().getLayoutInflater(); 
    builder.setView(inflater.inflate(R.layout.add_class_dialog, null)); 
    builder.setPositiveButton("Add", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      mListener.onDialogPositiveClick(AddClassDialogFragment.this); 
     } 
    }); 
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      mListener.onDialogNegativeClick(AddClassDialogFragment.this); 
     } 
    }); 
    return builder.create(); 
} 
} 
+2

당신이 잘못된 ID를 참고로하는 것에있다 ** editClassName ** findViewById를 –

답변

1

TextView의 가치를 얻는 이유는 레이아웃에 하드 코드 된 것입니다. android:text="Class: " 코드에 몇 가지 문제가 있습니다. 대화 상자의 레이아웃 인 경우 Activity에서 해당 대화 상자를 팽창시키지 않아야합니다. 다음은이를 구현하는 방법입니다. 활동에서 레이아웃을 팽창시킬 필요가 없습니다. 레이아웃은 대화 상자에서 팽창되어야하며 액티비티에는 레이아웃이 무엇인지 또는 레이아웃에 대한 지식이 없어야합니다. 어떤 활동이든간에 이 필요한 것은 콜백을 통해 이루어져야합니다.

package com.soulpatch.stackoverflow; 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends FragmentActivity implements AddClassDialogFragment.AddClassDialogListener { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final AddClassDialogFragment acdf = new AddClassDialogFragment(); 
    acdf.show(getSupportFragmentManager(), "abc"); 
} 

@Override 
public void onDialogPositiveClick(TextView textView, EditText editText) { 
    Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show(); 
    Toast.makeText(this, editText.getText() == null ? "" : editText.getText().toString(), Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onDialogNegativeClick(TextView textView, EditText editText) { 
    Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show(); 
    Toast.makeText(this, editText.getText() == null ? "" : editText.getText().toString(), Toast.LENGTH_SHORT).show(); 
} 
} 

그리고 DialogFragment 당신이 여기 선언 인터페이스를 통해 다시 활동에 갈 필요가 무엇인지 정의합니다. TextView와 EditText를 사용하도록 변경했습니다. 귀하의 요구 사항에 따라 변경할 수 있습니다. builder.setView (inflater.inflate (R :

package com.soulpatch.stackoverflow; 

import android.app.Activity;  
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 
import android.view.LayoutInflater; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class AddClassDialogFragment extends DialogFragment { 
public interface AddClassDialogListener { 
    void onDialogPositiveClick(TextView textView, EditText editText); 

    void onDialogNegativeClick(TextView textView, EditText editText); 
} 

AddClassDialogListener mListener; 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    try { 
     mListener = (AddClassDialogListener) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); 
    } 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    final LayoutInflater inflater = getActivity().getLayoutInflater(); 
    final LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.add_class_dialog, null); 
    final TextView textView = linearLayout.findViewById(R.id.viewClassName); 
    final EditText editText = linearLayout.findViewById(R.id.editClassName); 
    builder.setView(linearLayout); 
    builder.setPositiveButton("Add", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      mListener.onDialogPositiveClick(textView, editText); 
     } 
    }); 
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      mListener.onDialogNegativeClick(textView, editText); 
     } 
    }); 
    return builder.create(); 
} 
} 
+0

내가 선을 변경하여 다르게 조금 해결 (그리고 조금 더 간단) 동안 이것을 사용한다 .layout.add_class_dialog, null)); to : 보기 dialogView = inflater.inflate (R.layout.add_class_dialog, null); builder.setView (dialogView); 다음 선언문을 사용하여 edittext 데이터에 액세스 할 수 있습니다. final EditText editClassName = dialogView.findViewById (R.id.editNameD); 그리고 getText() 값을 onDialogPositiveClick 메소드의 인수로 지정하십시오. – 4rsenal

0

당신은 당신의 EDITTEXT에 대한 잘못된 ID를 언급했다.

public class MainActivity extends FragmentActivity implements AddClassDialogFragment.AddClassDialogListener { 

TextView nameView; 
EditText editName; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    View vi = getLayoutInflater().inflate(R.layout.add_class_dialog, null); 
    nameView = vi.findViewById(R.id.viewClassName); 
    editName = vi.findViewById(R.id.editClassName); 

} 
public void addClass(View view) { 
    AddClassDialogFragment acdf = new AddClassDialogFragment(); 
    acdf.show(getFragmentManager(), "ac"); 
} 
@Override 
public void onDialogPositiveClick(DialogFragment dialog) { 
    Toast.makeText(this, nameView.getText().toString(), Toast.LENGTH_SHORT).show(); 
    Toast.makeText(this, editName.getText().toString(), Toast.LENGTH_SHORT).show(); 

} 
@Override 
public void onDialogNegativeClick(DialogFragment dialog) { 
    Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show(); 
} 
}