简体   繁体   English

不能使用按钮重复editText中的文本

[英]can't repeat text in editText using a button

I have a simple activity in android with a button and an editText widget. 我在android中有一个带有按钮和editText小部件的简单活动。 I have referenced the resource for the button, created an instance variable for the button, and invoked the listener method on it. 我引用了按钮的资源,为按钮创建了一个实例变量,并在其上调用了listener方法。

When I run the application, the button prints to the edit text widget, but only once. 当我运行应用程序时,按钮会打印到编辑文本小部件,但只打印一次。 I cannot continue to type "0000000", which is the ideal output that I want, rather than being stuck on "0". 我不能继续键入“0000000”,这是我想要的理想输出,而不是卡在“0”上。

The code I have used is: 我使用的代码是:

public class MainActivity extends Activity implements OnClickListener {

Button btn1;
EditText text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn1 = (Button) findViewById(R.id.btn0);

    // set an onclick listener to listen for when the buttons are clicked
    toe.setOnClickListener(this);

    text = (EditText) findViewById(R.id.editTextSimple);

}

@Override
public void onClick(View v) {
    text.setText(btn1.getText().toString());

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Is there a method or a work around that I could implement in order to print more digits to the edit text view? 有没有可以实现的方法或解决方法,以便在编辑文本视图中打印更多数字?

Thank you for your help 谢谢您的帮助

There is no need to reinvent the wheel here. 这里没有必要重新发明轮子。 You don't even have to use direct Strings. 你甚至不必使用直接字符串。

Your onClick could simply be: 你的onClick可能只是:

@Override
public void onClick(View v) {
    text.append(btn.getText());
}

Also, I don't know what "toe" is in 另外,我不知道“脚趾”是什么

toe.setOnClickListener(this);

but you should be setting the clicklistener on btn1 但你应该在btn1上设置clicklistener

Replace 更换

text.setText(btn1.getText().toString());

with

String t=text.getText().toString();
text.setText(t+btn1.getText().toString())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM