我正在动态添加一些EditTexts,当点击一个按钮时,信息被输入并显示在文本视图上,我需要使所有的EditText视图不可见,我面临的问题是只添加了最后一个EditText变得无形。我试图设置一个标签来设置EditText不可见但它不起作用我错过了什么?
对此有任何帮助将不胜感激。
import java.util.ArrayList;
import java.util.List;
import com.juancho.workingout.R;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
public class TEST extends Fragment {
// Add EditText Dynamically
RelativeLayout containerLayout;
static int exercisesViews = 0; // initial value
public int totalExercisesViews = 15;
Context context;
private int counterExeNumber = 1;
private int margingTopEditTextInitial = 120; // default position
private int margingTopNumberAdd = 130;
private List<EditText> editTextListName = new ArrayList<>();
EditText EditTextName;
String nameResultCardio, nameResult1, nameResult2, nameResult3;
int myTagValueCardio;
int myTagValueYesCardioExe2, myTagValueYesCardioExe3;
boolean cardioViewEnable = false;
boolean exe2IfCardioViewEnable = false;
boolean exe3IfCardioViewEnable = false;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final View ViewCalendarEditNotesInfo = inflater.inflate(R.layout.logs_add_notes, null);
exercisesViews = 0; // Reset
containerLayout = (RelativeLayout) ViewCalendarEditNotesInfo.findViewById(R.id.layout1);
LogsAddNotes.isExercise1Enable = true;
LogsAddNotes.isExercise2Enable = true;
LogsAddNotes.isExercise3Enable = true;
final Button btnSave = (Button) ViewCalendarEditNotesInfo.findViewById(R.id.btnSave);
btnSave.setVisibility(View.VISIBLE);
if (LogsAddNotes.isExercise1Enable == true) {
// Set Views
editTextName(exercisesViews);
myTagValueCardio = (int) EditTextName.getTag();
cardioViewEnable = true;
}
// --------------------------------------------------------------------------------------------------------------------------------------------
if (LogsAddNotes.isExercise2Enable == true) {
exercisesViews++;
counterExeNumber++;
margingTopEditTextInitial = margingTopEditTextInitial + margingTopNumberAdd;
// Set Views
editTextName(exercisesViews);
// Get information entered
nameResult2 = EditTextName.getText().toString();
myTagValueYesCardioExe2 = (int) EditTextName.getTag();
exe2IfCardioViewEnable = true;
}
// --------------------------------------------------------------------------------------------------------------------------------------------
if (LogsAddNotes.isExercise3Enable == true) {
exercisesViews++;
counterExeNumber++;
margingTopEditTextInitial = margingTopEditTextInitial + margingTopNumberAdd;
// Set Views
editTextName(exercisesViews);
// Get information entered
nameResult3 = EditTextName.getText().toString();
myTagValueYesCardioExe3 = (int) EditTextName.getTag();
exe3IfCardioViewEnable = true;
}
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int index = 0;
while (index < editTextListName.size()) {
// --------------------------------------------------------------------------------------------------
if (cardioViewEnable == true && index == 0) {
nameResultCardio = editTextListName.get(index).getText().toString();
if (myTagValueCardio == index) {
// tToast("B1");
EditTextName.setVisibility(View.INVISIBLE);
}
}
// --------------------------------------------------------------------------------------------------
if (exe2IfCardioViewEnable == true && index == 1) {
nameResult2 = editTextListName.get(index).getText().toString();
if (myTagValueYesCardioExe2 == index) {
// tToast("B2");
EditTextName.setVisibility(View.INVISIBLE);
}
}
// --------------------------------------------------------------------------------------------------
if (exe3IfCardioViewEnable == true && index == 2) {
nameResult3 = editTextListName.get(index).getText().toString();
if (myTagValueYesCardioExe3 == index) {
// tToast(" B3");
EditTextName.setVisibility(View.INVISIBLE);
}
}
// more exercises HERE
index++;
} // end while loop
}// end Click
});
return ViewCalendarEditNotesInfo;
}// end Main
// -----------------------------------------------------------------------------------------
private EditText editTextName(int _intID) {
EditTextName = new EditText(getActivity());
EditTextName.setId(_intID);
EditTextName.setHint("Name");
// CharSequence hint = "\u200F" + editText.getHint();
// editText.setHint(hint);
// Maximum input chars allow
int maxLength = 10;
// Allow only Letters
EditTextName.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength), new InputFilter() {
public CharSequence filter(CharSequence src, int start, int end, Spanned dst, int dstart, int dend) {
if (src.equals("")) { // for backspace
return src;
}
if (src.toString().matches("[a-zA-Z ]+")) {
return src;
}
return "";
}
} });
editTextListName.add(EditTextName);
containerLayout.addView(EditTextName);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) EditTextName.getLayoutParams();
layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
layoutParams.setMargins(300, margingTopEditTextInitial, 750, 0);
// RelativeLayout.LayoutParams()
EditTextName.setLayoutParams(layoutParams);
// if you want to identify the created editTexts, set a tag, like below
// EditTextName[i].setTag("EditText" + editText.getId());
EditTextName.setTag(_intID);
// tToast("THE TAG= " + _intID);
return EditTextName;
}
}