Android 通过自定义键盘在编辑文本中输入文本
您好,我已经使用gridview在我的应用程序中创建了6x6 EditText(覆盖70%的屏幕)。所以我使用Adapter类将36个EditText放到gridview中。在下面,我安排了10个按钮(覆盖屏幕剩余的30%),命名为1-9位数字和C,以创建定制键盘。这样我可以在编辑文本中输入1-9位数字(我还禁用了软键盘)。但我的疑问是,如何获取光标所在的edittext的rowid,在该edittext中,我的号码(我按下的按钮1-9)应作为输入。最后,我必须检索所有的值。所以,我的问题是关于EditExt的ID和编号。任何帮助都将不胜感激。提前谢谢。 下面是我的代码Android 通过自定义键盘在编辑文本中输入文本,android,input,android-edittext,keypad,Android,Input,Android Edittext,Keypad,您好,我已经使用gridview在我的应用程序中创建了6x6 EditText(覆盖70%的屏幕)。所以我使用Adapter类将36个EditText放到gridview中。在下面,我安排了10个按钮(覆盖屏幕剩余的30%),命名为1-9位数字和C,以创建定制键盘。这样我可以在编辑文本中输入1-9位数字(我还禁用了软键盘)。但我的疑问是,如何获取光标所在的edittext的rowid,在该edittext中,我的号码(我按下的按钮1-9)应作为输入。最后,我必须检索所有的值。所以,我的问题是关于
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快照
我会这样做: 1。)将当前编辑的EditText存储在适配器类中,并为您的
EditText
实现一个视图.OnFocusChangeListener
。这可以检测是否进入/退出编辑模式
将此添加到适配器类:
private EditText current = null;
public EditText getCurrentEditText() {return current;}
并将此OnFocusChangeListener设置为所有编辑文本:
View.OnFocusChangeListener listener = new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View view, boolean hasFocus)
{
if(hasFocus)
current = (EditText) view;
}
};
然后将其添加到适配器类的getView()
方法中的文本视图中,如:
editText.setOnFocusChangeListener(listener);
2。)现在,在按钮OnClickListener
中,您可以访问当前编辑的EditText
。按钮“5”的示例:
private View.OnClickListener click = new View.OnClickListener()
{
public void onClick(View v)
{
Integer value = (Integer) v.getTag();
EditText current = adapter.getCurrentEditText();
if(current != null)
current.setText(current.getText().toString()+value);
}
}
编辑:
这些东西是如何结合在一起的,这可能有点令人困惑
在活动中,您可能会从适配器类创建一个实例。我们刚刚创建的适配器现在有一个名为getCurrentEditText()
的方法,它将返回对当前编辑的EditText的引用。因此,您不需要在活动类中拥有所有EditText的引用,您可以通过适配器访问它们
因此,在您的活动中,您可以这样做:
创建适配器的实例
adapter = new TextAdapter(this);
设置按钮:
button1 = (Button) findViewById(...);
button2 = (Button) findViewById(...);
button3 = (Button) findViewById(...);
...
要区分按钮,可以执行以下操作:
button1.setTag(1);
button2.setTag(2);
button3.setTag(3);
...
然后您可以简单地添加OnClickListener,我们刚刚创建的所有按钮
button1.setOnClickListener(click);
button2.setOnClickListener(click);
button3.setOnClickListener(click);
...
请注意,按钮可以看到适配器,因为它们都是活动的成员,适配器可以返回当前的编辑文本。非常感谢您的回复。我请求您在getView方法中编辑为editText.setOnFocusListener(listner),而不是textView。在实现这一点时,我还有一个疑问,即如何在适配器类中定义onClick方法,因为我已经在xml(main.xml)布局文件夹中创建了按钮。因此,我在adapter类中使用findViewById方法获得了它们的引用。但不知何故,它总是显示出错误。我在这方面做错了什么吗?若能提供代码片段,将不胜感激。请建议。提前谢谢。嗨,你很好:)!我改变了文本视图,我总是不小心把它们混在一起。您不需要在适配器类中实现onClick()。在您的活动中,您创建了适配器类的一个实例,并通过xml中的id找到了按钮,对吗?如果是这样,那么您可以在那里实现OnClickListener,并在通过findViewById()设置按钮后将其设置为按钮。我们之前在适配器类中添加了一个自定义方法,该方法可以返回活动的EditText。我知道,有点困惑,我试着更新我的答案。嘿,我用活动程序编辑了我的帖子。当我运行这个应用程序时,我得到了显示,但当我单击按钮时,它显示为“应用程序意外停止”。当我添加sop以签入logcat时,我在当前中得到空对象。我想原因是我在TextAdapter类中禁用了软键盘。所以,我猜它也会禁用编辑文本的焦点。因此焦点侦听器事件不会触发,因此当前每次都包含null对象。请推荐。嗨,巴厘岛,你能回复吗?奇怪的是,我甚至没有得到任何其他人的回应。我想我在使用自定义键盘时可能会遇到这个问题,因为很多成员抱怨这个隐藏的软键盘。我修改了所有资源,甚至修改了stackoverflow,但没有用。但是我想在我的应用程序中使用我自己的键盘。嗨!你能把堆栈跟踪贴出来吗?现在你让我好奇了,所以我将尝试重新创建这个问题:)