按键仅点击一次,自定义视图 [英] Button only clickable once with Custom View
问题描述
我做了定制图像查看,简单的线条绘制。此图是由一个按钮triggeed。 当触发onClicklistener心不是工作了。但在屏幕上的按钮保持点击,点击时(默认的Android按钮)的按钮改变为略带蓝色的颜色。我认为这个问题可能是在的drawLine()当的setContentView(R.layout.activity_main);是所谓的,但不知道为什么,以及如何摆脱它。希望能帮到你。
MainActivity.java
公共类MainActivity扩展ActionBarActivity实现OnTouchListener,
OnClickListener {
私人诠释数目;
私人处理程序处理程序;
私人布尔运行=真;
私人诠释endX = 50;
私人诠释恩迪= 500;
私人诠释STARTX = 50;
私人诠释startY = 50;
私人诠释帧= 25;
ImageView的Line01;
按钮buttonLineDrawer;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
Line01 =(ImageView的)findViewById(R.id.Line01);
Line01.setVisibility(View.GONE);
按钮buttonLineDrawer;
buttonLineDrawer =(按钮)findViewById(R.id.buttonLineDrawer);
buttonLineDrawer.setOnClickListener(本);
}
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
// TODO自动生成方法存根
返回false;
}
@覆盖
公共无效的onClick(视图v){
Toast.makeText(getApplicationContext(),克利克,Toast.LENGTH_LONG)
。显示();
运行=真;
号= 0;
的drawLine();
}
公共无效的drawLine(){
Line01.setVisibility(View.VISIBLE);
处理程序=新的处理程序();
可运行可运行=新的Runnable(){
@覆盖
公共无效的run(){
而(运行){
尝试 {
视频下载(40);
}赶上(InterruptedException异常E){
e.printStackTrace();
}
handler.post(新的Runnable(){
@覆盖
公共无效的run(){
INT coordX =((((endX - STARTX)/帧)*号))
+ STARTX;
INT coordY =((((恩迪 - startY)/帧)*号))
+ startY;
号+ = 1;
CustomDraw.setCoordinates(STARTX,startY,coordX,
coordY);
的setContentView(R.layout.activity_main);
如果((coordX - endX)== 0安培;&安培; coordY - 恩迪== 0){
运行= FALSE;
}
}
});
}
}
};
新的线程(可运行)。开始();
}}
activity_main.xml
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:后台=#FF8800
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:以下属性来=@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=de.trialar.linedrawer.MainActivity>
<的LinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直>
<按钮
机器人:ID =@ + ID / buttonLineDrawer
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=按钮/>
< de.trialar.linedrawer.CustomDraw
机器人:ID =@ + ID / Line01
机器人:layout_width =400dp
机器人:layout_height =400dp/>
< / LinearLayout中>< / RelativeLayout的>
CustomDraw.java
公共类CustomDraw扩展ImageView的{
涂料粉刷=新的油漆();
私有静态诠释endX = 500;
私有静态诠释恩迪= 500;
私有静态诠释STARTX = 50;
私有静态诠释startY = 50;
静态上下文的背景下;
公共CustomDraw(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
超(背景下,ATTRS,defStyle);
this.context =背景;
}
公共CustomDraw(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
}
公共CustomDraw(上下文的背景下){
超(上下文);
}
@覆盖
保护无效的OnDraw(帆布油画){
super.onDraw(画布);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(10);
canvas.drawLine(STARTX,startY,endX,恩迪,油漆);
}
公共静态无效setCoordinates(INT STARTX,诠释startY,诠释endX,诠释恩迪){
的System.out.println(SetCoordinates);
CustomDraw.endX = endX;
CustomDraw.endY =恩迪;
CustomDraw.startX = STARTX;
CustomDraw.startY = startY;
}}
我想我发现了问题:当您启动handler.run()设置一个新的布局:
公共无效的run(){
INT coordX =((((endX - STARTX)/帧)*号))+ STARTX;
INT coordY =((((恩迪 - startY)/帧)*号))+ startY;
号+ = 1;
CustomDraw.setCoordinates(STARTX,startY,coordX,coordY);
//也许这是错误的
的setContentView(R.layout.activity_main);
//尝试,而不是:
CustomDraw.invalidate();
如果((coordX - endX)== 0安培;&安培; coordY - 恩迪== 0){
运行= FALSE;
}
}
我不明白你为什么设定一个新的布局......我猜你要重新绘制按钮...这就是为什么我添加了一个repaint'线在code-件以上。
i made custom Image View, where a simple line is drawn. This drawing is triggeed by a button. When triggered the onClicklistener isnt working anymore. But on the screen the button remains clickable, the buttons changes to a blueish color when clicked (default Android button). I think the problem might be in the "drawLine()" when the "setContentView(R.layout.activity_main);" is called, but not sure why and how to get rid of it. Hope you can help.
MainActivity.java
public class MainActivity extends ActionBarActivity implements OnTouchListener,
OnClickListener {
private int number;
private Handler handler;
private boolean Running = true;
private int endX = 50;
private int endY = 500;
private int startX = 50;
private int startY = 50;
private int frames = 25;
ImageView Line01;
Button buttonLineDrawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Line01 = (ImageView) findViewById(R.id.Line01);
Line01.setVisibility(View.GONE);
Button buttonLineDrawer;
buttonLineDrawer = (Button) findViewById(R.id.buttonLineDrawer);
buttonLineDrawer.setOnClickListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "klick", Toast.LENGTH_LONG)
.show();
Running = true;
number = 0;
drawLine();
}
public void drawLine() {
Line01.setVisibility(View.VISIBLE);
handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
while (Running) {
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
int coordX = ((((endX - startX) / frames) * number))
+ startX;
int coordY = ((((endY - startY) / frames) * number))
+ startY;
number += 1;
CustomDraw.setCoordinates(startX, startY, coordX,
coordY);
setContentView(R.layout.activity_main);
if ((coordX - endX) == 0 && coordY - endY == 0) {
Running = false;
}
}
});
}
}
};
new Thread(runnable).start();
}}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF8800"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.trialar.linedrawer.MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/buttonLineDrawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<de.trialar.linedrawer.CustomDraw
android:id="@+id/Line01"
android:layout_width="400dp"
android:layout_height="400dp" />
</LinearLayout></RelativeLayout>
CustomDraw.java
public class CustomDraw extends ImageView {
Paint paint = new Paint();
private static int endX= 500;
private static int endY= 500;
private static int startX= 50;
private static int startY= 50;
static Context context;
public CustomDraw(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
public CustomDraw(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomDraw(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(10);
canvas.drawLine(startX, startY, endX, endY, paint);
}
public static void setCoordinates(int startX, int startY, int endX, int endY) {
System.out.println("SetCoordinates");
CustomDraw.endX = endX;
CustomDraw.endY = endY;
CustomDraw.startX = startX;
CustomDraw.startY = startY;
}}
i think i found the problem: when you start the handler.run() you set a new Layout:
public void run() {
int coordX = ((((endX - startX) / frames) * number)) + startX;
int coordY = ((((endY - startY) / frames) * number)) + startY;
number += 1;
CustomDraw.setCoordinates(startX, startY, coordX, coordY);
//maybe this is wrong
setContentView(R.layout.activity_main);
//try instead:
CustomDraw.invalidate();
if ((coordX - endX) == 0 && coordY - endY == 0) {
Running = false;
}
}
i don't get why you set a new layout... i guess you want to repaint the button... that's why i added a 'repaint'-line in that code-piece above
这篇关于按键仅点击一次,自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!