我在android上制作一个数独拼图应用程序。这就是我到目前为止所做的事情 Text Adapter.java
public class TextAdapter extends BaseAdapter {
private Context mContext;
int count = 81;
int k = 1;
private static EditText current = null;
public TextAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 81;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final EditText editText;
if (convertView == null) {
editText = new EditText(mContext);
editText.setLayoutParams(new GridView.LayoutParams(85, 85));
editText.setPadding(8, 8, 8, 8);
} else {
editText = (EditText) convertView;
}
editText.setId(k);
editText.setGravity(Gravity.CENTER);
editText.setBackgroundResource(R.drawable.rectangle);
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(1);
editText.setFilters(FilterArray);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
// TODO Auto-generated method stub
editText.setInputType(InputType.TYPE_NULL);
if (current != null) {
current.setBackgroundResource(R.drawable.rectangle);
}
if (hasFocus) {
current = (EditText) view;
editText.setBackgroundResource(R.drawable.rectangleblue);
}
}
});
return editText;
}
public static EditText getCurrentEditText() {
return current;
}
}
我想用1-9中的数字随机填充一些edittexts,但我不知道如何获取每个edittext的标识符/名称。
这可能吗?怎么样?
我是android开发的新手。
答案 0 :(得分:0)
创建EditText时,将它们添加到父级。
parent.addView(editText);
每当你想获得已经创建的EditTexts时,你可以遍历父母的孩子:
Edittext tempET;
int tempID=0;
for(int i = 0 ; i < parent.getChildCount(); i++) {
tempET=(EditText) parent.getChildAt(i);
tempID=tempET.getId();
}