我的imageview事物不斷關閉我。Button保持對我的關閉?
XML代碼:這是我的代碼放在按鈕
<Button
android:id="@+id/sound"
android:src="@drawable/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Java代碼:在那裏我有按鈕的代碼和它說image1.setOnClickListener(this);
是在logcat中關閉它就是力量。
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(this);
}
public void onClick(View v) {
switch(v.getId()){
}
}
我的整個代碼:如果你需要看到它,所以你可以告訴什麼在
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;
}
}
你發佈logcat時發生了什麼錯誤。 –