繁体   English   中英

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

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

我在android中有一个带有按钮和editText小部件的简单活动。 我引用了按钮的资源,为按钮创建了一个实例变量,并在其上调用了listener方法。

当我运行应用程序时,按钮会打印到编辑文本小部件,但只打印一次。 我不能继续键入“0000000”,这是我想要的理想输出,而不是卡在“0”上。

我使用的代码是:

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;
}

}

有没有可以实现的方法或解决方法,以便在编辑文本视图中打印更多数字?

谢谢您的帮助

这里没有必要重新发明轮子。 你甚至不必使用直接字符串。

你的onClick可能只是:

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

另外,我不知道“脚趾”是什么

toe.setOnClickListener(this);

但你应该在btn1上设置clicklistener

更换

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

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