将editText添加到片段

时间:2016-01-19 18:56:25

标签: android android-fragments

我正在尝试将editText和textView添加到片段中。生成editText和TExtView的代码是正确的,因为我之前使用过它,但在这种情况下没有出现。有什么想法吗?

片段代码:

public class MarcadoresFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View myFragmentView = inflater.inflate(R.layout.fragment_marcadores, container, false);
    return myFragmentView;
}

}

添加editText:

public void createEditText(int size){
    Log.d("det", String.valueOf(size));

    LayoutInflater inflater = this.getLayoutInflater();

    View dialogView = inflater.inflate(R.layout.fragment_marcadores, null);

    LinearLayout layout = (LinearLayout) dialogView.findViewById(R.id.marcadoresFrag); // this is whatever view you want to add them to

    EditText editText;
    TextView textView;

    if(layout!=null)
        for(int i=0;i<size;i++){

            textView = new TextView(dialogView.getContext());
            textView.setText("Descrição do marcador " + String.valueOf(i + 1));

            textView.layout(0, 5, 0, 5);

            // add to the view
            layout.addView(textView);

            editText = new EditText(dialogView.getContext());

            editText.setId(i);
            editText.setHint("Ex: Bebedouro ndsfhjdsjvdsbjvjhvjdfjbvfjfvjklbjklfv" + i + 1);

            Log.d("det", "jsdgbiusdbgksjd");

            allEds.add(editText);

            // add to the view
            layout.addView(editText);
        }
}

0 个答案:

没有答案