Java 巴顿一直逼着我?
我的imageview一直在逼近我 XML代码:这是我放置按钮的代码Java 巴顿一直逼着我?,java,android,Java,Android,我的imageview一直在逼近我 XML代码:这是我放置按钮的代码 <Button android:id="@+id/sound" android:src="@drawable/test" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 我的全部代码是:如果你需要看它,这样你就可以知道发生了什么 package dev.mrunkno
<Button
android:id="@+id/sound"
android:src="@drawable/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
我的全部代码是:如果你需要看它,这样你就可以知道发生了什么
package dev.mrunknow.slidedirection;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
public class SlideDirection extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
private LinearLayout main;
private TextView viewA;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main = new LinearLayout(this);
main.setBackgroundColor(Color.BLACK);
main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
viewA = new TextView(this);
viewA.setBackgroundColor(Color.WHITE);
viewA.setTextColor(Color.BLACK);
viewA.setTextSize(15);
viewA.setLayoutParams(new LinearLayout.LayoutParams(320,180));
main.addView(viewA);
setContentView(main);
Button image1 = (Button) findViewById(R.id.sound);
image1.setOnClickListener(SlideDirection.this);
}
public void onClick(View v) {
switch(v.getId()){
}
}
float x_start = 0, y_start = 0, x_end = 0, y_end = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
viewA.setText("");
viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
viewA.setTextSize(40);
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
{
x_start = event.getX();
y_start = event.getY();
}
if(action == MotionEvent.ACTION_UP)
{
x_end = event.getX();
y_end = event.getY();
if((x_start - x_end) > 75 && (y_start - y_end) < -75)
{
viewA.setText("LEFT");
Toast.makeText(this, "Left Works!", Toast.LENGTH_SHORT).show();
}
if((x_start - x_end) < -75 && (y_start - y_end) < -75)
{
viewA.setText("RIGHT");
Toast.makeText(this, "Right Works!", Toast.LENGTH_SHORT).show();
}
}
return true;
}
}
包dev.mrunknow.slidedirection;
导入android.app.Activity;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.view.MotionEvent;
导入android.widget.Button;
导入android.widget.ImageButton;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.widget.TextView;
导入android.widget.Toast;
导入android.view.view;
公共类SlideDirection扩展活动实现View.OnClickListener{
/**在首次创建活动时调用*/
私人线路主配线架;
私有文本视图视图a;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
main=新的线性布局(本);
主色.背景色(颜色.黑色);
main.setLayoutParams(新的LinearLayout.LayoutParams(320480));
viewA=新文本视图(此);
viewA.setBackgroundColor(颜色:白色);
viewA.setTextColor(Color.BLACK);
viewA.setTextSize(15);
viewA.setLayoutParams(新的LinearLayout.LayoutParams(320180));
main.addView(viewA);
setContentView(主);
按钮图像1=(按钮)findViewById(R.id.sound);
image1.setOnClickListener(SlideDirection.this);
}
公共void onClick(视图v){
开关(v.getId()){
}
}
浮动x_开始=0,y_开始=0,x_结束=0,y_结束=0;
@凌驾
公共布尔onTouchEvent(运动事件){
viewA.SETEXT(“”);
viewA.setLayoutParams(新的LinearLayout.LayoutParams(320,80));
viewA.setTextSize(40);
int action=event.getAction();
if(action==MotionEvent.action\u DOWN)
{
x_start=event.getX();
y_start=event.getY();
}
if(action==MotionEvent.action\u UP)
{
x_end=event.getX();
y_end=event.getY();
如果((x_开始-x_结束)>75&(y_开始-y_结束)<-75)
{
viewA.setText(“左”);
Toast.makeText(这个“左起作用!”,Toast.LENGTH_SHORT.show();
}
如果((x_开始-x_结束)<-75&(y_开始-y_结束)<-75)
{
viewA.setText(“右”);
Toast.makeText(这个“Right Works!”,Toast.LENGTH_SHORT.show();
}
}
返回true;
}
}
为什么在扩展的同时实现OnClickListener
而冒险。。为什么不创建这样一个内部类呢
class click implements OnCLickListener{
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stubdo your stuff
}
}
而不是image1.setOnClickListener(SlideDirection.this)强>
添加
image1.setOnClickListener(新建click())代码>请更改onCreate()方法,如下所示,它工作正常。
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState)
当您动态创建“活动”视图时,您没有在“活动”视图中添加任何按钮,因此它将始终返回null,因此,将引发异常,应用程序将崩溃…您在日志中遇到的错误。
class click implements OnCLickListener{
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stubdo your stuff
}
}
public void onClick(View v) {
if(v==image1)
{
//your logic
}
}
main = new LinearLayout(this);
main.setBackgroundColor(Color.BLACK);
main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
viewA = new TextView(this);
viewA.setBackgroundColor(Color.WHITE);
viewA.setTextColor(Color.BLACK);
viewA.setTextSize(15);
viewA.setLayoutParams(new LinearLayout.LayoutParams(320,180));
// add button programmatically not from xml the change that i have suggest you
image1 = new Button(this);
image1.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon));
image1.setLayoutParams(new LinearLayout.LayoutParams(320,180));
main.addView(viewA);
main.addView(image1);
setContentView(main);
// Button image1 = (Button)findViewById(R.id.sound);
image1.setOnClickListener(this);
}