Android 用户输入,使用自定义键盘
对于初学者来说,任何帮助将不胜感激!我创建了一个数字键盘,每次选择其中一个按钮时,我都需要将数值传递给 EditText。我遇到一个问题,每次按下不同的按钮时,EditText 都会被 setText 覆盖。我确实需要连接每个值,但我不太确定如何做到这一点。可以按任何顺序按下一到九个按钮中的任何一个。
这是一些代码。我只是想先让这些键发挥作用。
View hash = findViewById(R.id.keypad_hash);
hash.setOnClickListener(this);
View key1 = findViewById(R.id.keypad_1);
key1.setOnClickListener(this);
View key2 = findViewById(R.id.keypad_2);
key2.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.keypad_hash:
questions();
break;
case R.id.keypad_1:
final EditText number_edit_text1 = (EditText) this.findViewById(R.id.Edit);
number_edit_text1.setText(String.valueOf("1"));
break;
case R.id.keypad_2:
final EditText number_edit_text2 = (EditText) this.findViewById(R.id.Edit);
number_edit_text2.setText(String.valueOf("2"));
break;
}
}
然后是布局中的编辑文本
<EditText
android:id="@+id/Edit"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_width="fill_parent"
android:numeric="integer">
</EditText>
For starters, any help would be greatly appreciated! I have created a numerical keypad and I need to pass the numeric values to an EditText every time one of the buttons is selected. I'm running into a problem where the EditText is being overwritten by setText, every time a different button is pressed. I really need to concatenate each value, and I'm not quite sure how to do this. Any of the one to nine buttons could be pressed in any order.
Here is some of the code. I'm just trying to get these keys working first.
View hash = findViewById(R.id.keypad_hash);
hash.setOnClickListener(this);
View key1 = findViewById(R.id.keypad_1);
key1.setOnClickListener(this);
View key2 = findViewById(R.id.keypad_2);
key2.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.keypad_hash:
questions();
break;
case R.id.keypad_1:
final EditText number_edit_text1 = (EditText) this.findViewById(R.id.Edit);
number_edit_text1.setText(String.valueOf("1"));
break;
case R.id.keypad_2:
final EditText number_edit_text2 = (EditText) this.findViewById(R.id.Edit);
number_edit_text2.setText(String.valueOf("2"));
break;
}
}
and then the edittext in the layout
<EditText
android:id="@+id/Edit"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_width="fill_parent"
android:numeric="integer">
</EditText>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
如果由于某种原因不起作用:
另外,顺便说一下,您可以自己让这个过程变得更容易。
在您的 xml 中,您应该执行以下操作:
并将 0 替换为每个按钮所需的任何数字。
在类主体中,您应该声明
EditText editText;
,然后在onCreate
中,您应该执行editText = (EditText)findViewById(R.id.Edit);
然后在
onClick
中,只需执行以下操作:这应该会简化您的代码,使其更易于管理,并使用稍微更少的资源,因为您不必重复重新创建
EditText
。Try:
If that for some reason doesn't work:
Also, on a side note, you can make the process easier on yourself.
In your xml, you should do:
and replace 0 with whatever number you want for each button.
In your class body, you should declare
EditText editText;
, then inonCreate
, you should doeditText = (EditText)findViewById(R.id.Edit);
Then in
onClick
, just do:This should simplify your code, make it more manageable, and use ever so slightly fewer resources since you don't have to repeatedly recreate the
EditText
.