onDraw() 中的编辑文本
我知道如何添加 EditText 但不知道如何在 onDraw() 函数中添加。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GaugeAnimation((this),10));
EditText input = (EditText) findViewById(R.id.input);
input.setText("hello");
}
上面的方法不起作用,下面的方法也不起作用:
@Override
public void onDraw(Canvas c){
EditText input = (EditText) findViewById(R.id.input);
input.setText("hi");
}
那么我该怎么做呢?我基本上想添加一个可编辑的文本框,但因为我没有使用:
setContentView(new R.layout.main);
它搞砸了。有什么建议吗?
我尝试过:
public class GaugeAnimation extends View{
public GaugeAnimation(Context context, int value){
EditText input = new EditText(context);
input.setId(R.id.input);
}
}
但仍然不起作用,我做错了什么?
I am aware of how to add EditText but not in the onDraw() function.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GaugeAnimation((this),10));
EditText input = (EditText) findViewById(R.id.input);
input.setText("hello");
}
The above will not work, also, the below doesn't work either:
@Override
public void onDraw(Canvas c){
EditText input = (EditText) findViewById(R.id.input);
input.setText("hi");
}
so how can i do this? i would basically like to add an editable text box but because i am not using:
setContentView(new R.layout.main);
its messing up. Any suggestions?
i tried:
public class GaugeAnimation extends View{
public GaugeAnimation(Context context, int value){
EditText input = new EditText(context);
input.setId(R.id.input);
}
}
but it still doesn't work what am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您构造
GaugeAnimation
视图时,请确保它为要用于输入的 EditText 视图调用setId(R.id.input)
。那么你的第一个方法就可以工作了。您应该注意,您不应该像您尝试的那样从视图的
onDraw
方法更新 UI 元素。When you construct your
GaugeAnimation
view, make sure it callssetId(R.id.input)
for the EditText view you want to use for input. Then your first method will work.You should be aware that you should not make updates to UI elements from a view's
onDraw
method like you tried.我想你必须将编辑文本膨胀到视图才能使用它。如果没有膨胀,您的编辑文本还没有实例化。
I guess you have to inflate the edittext to a view to be able to use it. Without inflating, your edittext isnt instantiated yet.