Android的按键仅适用于第二次点击 [英] Android Button Works Only on the second Click

查看:132
本文介绍了Android的按键仅适用于第二次点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展我的第一款Android计算器应用。我被困在一个单一的缺陷。我增加了几个按钮,在单击这些按钮,就会把对的EditText 字段respectives文本。主要的问题是描述如下,
              当运行项目,按钮必须被点击了两次穿上的EditText 字段相应文本首次。例如, Button1的看跌期权上点击的EditText 字段'1'。当上运行时,该按钮首先点击什么也不做。只有在第二次点击它把'1'上的的EditText 字段。

在code以下,

XML按钮和EditField中,

 <的EditText
        机器人:ID =@ + ID / textView1
        机器人:layout_width =match_parent
        机器人:layout_height =60dp
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:ellipsize =结束
        机器人:EMS =10
        机器人:重力=右
        机器人:提示=@字符串/ textView1/>
 <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =60dp
        机器人:layout_height =60dp
        机器人:提示=@字符串/按钮1
        安卓的onClick =SET1/>


MainActivity.java

有关按钮的onClick相应的功能,

 公共无效SET1(视图v){
    最终按钮按钮1 =(按钮)findViewById(R.id.button1);
    button1.setOnClickListener(新OnClickListener(){            公共无效的onClick(视图v)
            {
                EditText上电视=(EditText上)findViewById(R.id.textView1);
                字符串文本= tv.getText()的toString()。
                tv.setText(文字+1);
            }
    });}


解决方案

更改 SET1()方法如下,

 公共无效SET1(视图V)
{
    EditText上电视=(EditText上)findViewById(R.id.textView1);
    字符串文本= tv.getText()的toString()。
    tv.setText(文字+1);
}

I am Developing my first android Calculator app. I'm stuck with a single defect. I have added few buttons and on clicking those buttons, it will put the respectives text on the EditText Field. The Main Problem is described below, When on running the project, the buttons have to be clicked twice to put the respective text on the EditText field for the first time. For example, Button1 puts '1' on the EditText field on click. When on run, First click on that button does nothing. Only on the second click it puts '1' on the EditText field.

The Code follows,

XML Button and EditField,

<EditText
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ellipsize="end"
        android:ems="10"
        android:gravity="right"
        android:hint="@string/textView1" />
 <Button
        android:id="@+id/button1"        
        android:layout_width="60dp"       
        android:layout_height="60dp"
        android:hint="@string/button1"
        android:onClick="set1" />


MainActivity.java

The respective function for Button onClick,

public void set1(View v){
    final Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener(){

            public void onClick(View v)
            {
                EditText tv = (EditText) findViewById(R.id.textView1);
                String text=tv.getText().toString();
                tv.setText(text+"1");
            }
    });

}

解决方案

Change your set1() method as follows,

public void set1(View v)
{
    EditText tv = (EditText) findViewById(R.id.textView1);
    String text=tv.getText().toString();
    tv.setText(text+"1");
}

这篇关于Android的按键仅适用于第二次点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆