嗨,我使用 gridview 在我的应用程序中创建了 6x6 编辑文本(覆盖了 70% 的屏幕)。所以我使用 Adapter 类将这 36 个编辑文本放在 gridview 中。下面我安排了 10 个按钮(覆盖剩余 30% 的屏幕),命名为 1-9 位和 C,以创建自定义键盘。这样我就可以在编辑文本中输入 1-9 位数字(我还禁用了软键盘)。但我的疑问是如何获取光标所在的edittext的rowid,并且在该edittext中,我的数字(我按下的(按钮1-9))应该作为输入。最后,我必须检索所有值。所以,我的问题是关于editexts 的ID 和号码放置。任何帮助将不胜感激。提前致谢。下面是我的代码。
public class TextAdapter extends BaseAdapter {
private Context mContext;
int count=36;
int k=0;
public TextAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
final EditText editText;
if (convertView == null) { // if it's not recycled, initialize some attributes
editText = new EditText(mContext);
editText.setLayoutParams(new GridView.LayoutParams(54, 53));
editText.setBackgroundResource(R.drawable.edittextshape);
editText.setGravity(Gravity.CENTER);
editText.setId(k);
k++;
editText.setFilters( new InputFilter[] { new InputFilter.LengthFilter(1)});
editText.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int inType = editText.getInputType(); // backup the input type
editText.setInputType(InputType.TYPE_NULL); // disable soft
editText.onTouchEvent(event); // call native handler
editText.setInputType(inType); // restore input type
return true; // consume touch even
}
});
// editText.setScaleType(EditText.ScaleType.CENTER_CROP);
editText.setPadding(0, 0, 0, 0);
} else {
editText = (EditText) convertView;
}
editText.setText(" ");
return editText;
}
}
活动计划:
public class MainActivity extends Activity implements OnClickListener{
GridView gridView;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
Button b9;
Button b10;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_two_frame_layout);
FrameLayout fL1 = (FrameLayout) findViewById(R.id.fLayout1);
gridView = (GridView) findViewById(R.id.gridview);
gridView.setVerticalScrollBarEnabled(false);
gridView.setAdapter(new TextAdapter(this));
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int gridSize = display.getWidth();
int colWidth = (gridSize / 9);
gridView.setColumnWidth(colWidth);
gridView.setNumColumns(9);
b1=(Button)findViewById(R.id.keypad_1);
b2=(Button)findViewById(R.id.keypad_2);
b3=(Button)findViewById(R.id.keypad_3);
b4=(Button)findViewById(R.id.keypad_4);
b5=(Button)findViewById(R.id.keypad_5);
b6=(Button)findViewById(R.id.keypad_6);
b7=(Button)findViewById(R.id.keypad_7);
b8=(Button)findViewById(R.id.keypad_8);
b9=(Button)findViewById(R.id.keypad_9);
b10=(Button)findViewById(R.id.keypad_10);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b10.setOnClickListener(this);
}
TextAdapter textadapter=new TextAdapter(this);
public void onClick(View v)
{
EditText current = textadapter.getCurrentEditText();
System.out.println(textadapter);
System.out.println(current);
switch (v.getId())
{
case R.id.keypad_1:
current.setText("1");
break;
case R.id.keypad_2:
current.setText(current.getText().toString()+'2');
break;
case R.id.keypad_3:
if(current != null)
current.setText(current.getText().toString()+'3');
break;
case R.id.keypad_4:
if(current != null)
current.setText(current.getText().toString()+'4');
break;
case R.id.keypad_5:
if(current != null)
current.setText(current.getText().toString()+'5');
break;
case R.id.keypad_6:
if(current != null)
current.setText(current.getText().toString()+'6');
break;
case R.id.keypad_7:
if(current != null)
current.setText(current.getText().toString()+'7');
break;
case R.id.keypad_8:
if(current != null)
current.setText(current.getText().toString()+'8');
break;
case R.id.keypad_9:
if(current != null)
current.setText(current.getText().toString()+'9');
break;
case R.id.keypad_10:
if(current != null)
current.setText("");
break;
}
}
}
下面是 logcat 截图。