我想知道用户是否改变了焦点并从一个editText转到了另一个。如果是这种情况,那么将通过ScrollView中的代码动态创建editTextes(和TextViews),因此在需要时会给出ID,editTextes可能是数百个。
我的想法是将这些ID存储在ArrayList
上,然后执行循环并检查具有ArrayList中ID的editText是否改变了焦点。不幸的是,我的代码仅适用于已从activity_main.xml
初始化的ArrayList中的第一项。为什么我的代码对ArrayList的其余ID无效?
部分代码如下:
MainActivity.java
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private GridLayout mlayout;
//Creating ArrayList which holds all IDs of editTexts
public static List<Integer> list = new ArrayList<>();
Button additem;
DynamicViews dnv;
Context context;
int f=1;
int counter = 2;
View view;
EditText edit1;
TextView test;
int trent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mlayout = (GridLayout) findViewById(R.id.mylayout);
additem = (Button) findViewById(R.id.addButton);
edit1 = (EditText) findViewById(R.id.code1);
test = (TextView) findViewById(R.id.textTest);
//When ListArray's size is 0 I am inserting the first ID of EditText from .xml
if (list.size()==0) {
list.add(R.id.code1);
}
//Do a loop of ListArray according to the size, if focus of EditText changed then add EditText and TextView
for (int v = 0; v<list.size(); v++){
findViewById(list.get(v)).setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
addView(view);
counter+=2;
}
}
});
}
}
public void addView (View view)
{
this.view=view;
f=f+1;
dnv = new DynamicViews(context);
mlayout.addView(dnv.descriptopionTextView(getApplicationContext(),Integer.toString(f)),counter);
mlayout.addView(dnv.receivedQuantityEditText(getApplicationContext(),f),counter+1);
}
}
DynamicViews.java
//This is the class where I am creating dynamically TextView and EditText
public class DynamicViews {
Context ctx;
public DynamicViews(Context ctx) {
this.ctx = ctx;
}
public TextView descriptopionTextView(Context context,String text) {
final ViewGroup.LayoutParams lParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(context);
textView.setLayoutParams(lParams);
textView.setTextSize(16);
textView.setTypeface(null, Typeface.BOLD);
textView.setTextColor(Color.rgb(255,255,255));
textView.setText(" "+ text+". ");
return textView;
}
public EditText receivedQuantityEditText(Context context,int fin) {
final ViewGroup.LayoutParams lParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final EditText editText = new EditText(context);
//I am adding the fin variable in ArrayList and then as ID. So the ID is now also inside the ArrayList
MainActivity.list.add(fin);
editText.setId(fin);
editText.setTextSize(TypedValue.COMPLEX_UNIT_SP,16f);
editText.setTextColor(Color.rgb(255,255,255));
DrawableCompat.setTint(editText.getBackground(), ContextCompat.getColor(context, R.color.colorPrimary));
editText.setInputType(TYPE_CLASS_NUMBER);
return editText;
}
}