こんにちは、グリッドビューを使用してアプリケーションで 6x6 の編集テキスト (画面の 70% をカバー) を作成しました。そこで、Adapter クラスを使用して、その 36 個の編集テキストをグリッドビューに配置しました。その下に、カスタマイズされたキーパッドを作成するために、1 ~ 9 の数字と C という名前の 10 個のボタン (画面の残りの 30% を占める) を配置しました。edittexts に 1 ~ 9 桁を入力できるようにします (ソフトキーパッドも無効にしました)。しかし、私の疑問は、カーソルが存在する編集テキストの行 ID を取得する方法であり、その編集テキストでは、私の番号 ((ボタン 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ショットです。