How can I detect focused EditText in android?(如何在 android 中检测聚焦的 EditText?)
问题描述
我的页面中有许多 EditText
元素以及两个按钮.我希望用户触摸任何一个 EditText
字段并单击任何按钮以将某个值插入到他们触摸的那个 EditText
字段中.不允许使用键盘进行输入.请帮我做这件事.
I have a number of EditText
elements in my page along with two buttons. I want the user to touch on any one EditText
field and click any button to insert a certain value into that very EditText
field they touched. Giving input using the keypad is not allowed. Please help me to do this.
推荐答案
你可以做的一件事是声明一个全局变量evalue
,它会告诉你哪个是最后选择的EditText
通过使用onTouchListener
,然后根据evalue
的值,可以通过按钮点击将文本值设置为edittext.希望你能理解.
One thing that you can do is declare a global variable evalue
which will tell you which is the last selected EditText
by using onTouchListener
and then based on the value of evalue
, you can set the text value to the edittext by button click. hope you understood.
代码如下:
EditText e1,e2;
Button b1,b2;
String evalue;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
e1.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1)
{
evalue="1";
return false;
}
});
e2.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1)
{
evalue="2";
return false;
}
});
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0) {
if(evalue=="1")
{
e1.setText("yes");
}
if(evalue=="2")
{
e2.setText("yes");
}
}
});
b2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0) {
if(evalue=="1")
{
e1.setText("No");
}
if(evalue=="2")
{
e2.setText("No");
}
}
});
}
这是一个逻辑编码.. 不符合标准.. 如果您找到更好的编码.然后使用它.谢谢.
Its a logical coding.. not upto the mark.. if you find a better one. then use it. thank you.
这篇关于如何在 android 中检测聚焦的 EditText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 android 中检测聚焦的 EditText?


- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01