在android中的背景图像上单击事件

时间:2013-09-13 17:22:35

标签: android onclick

我在点击屏幕上放置点击事件

<TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:onClick="click_screen" />

另一个活动的.java文件(click_screen)

public void click_screen(View v)
{
    Intent click_screen=new Intent(this, MainActivity.class);
    startActivity(click_screen);
}

MainActivity.java

package com.amcct.amcostapp;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity 
{

    @SuppressLint("NewApi")
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

/*  @Override
    public boolean onTouchEvent(MotionEvent event) 
    {
        if (event.getAction() == MotionEvent.ACTION_DOWN) 
        {
            Intent click_screen=new Intent(this, MainActivity.class);
            startActivity(click_screen);
        }

        return super.onTouchEvent(event);
    }*/

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

    public void click_screen(View v)
    {
        Intent click_screen=new Intent(this, MainActivity.class);
        startActivity(click_screen);
    }
}

=============================================== ====================== .xml当我点击它生成^^以上click_screen

的任何地方
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:onClick="click_screen"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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=".Click_Page"
    android:background="@drawable/img_1" >

</RelativeLayout>

看到我的所有文件......

5 个答案:

答案 0 :(得分:0)

尝试为xml的父视图设置onClick。像.-

之类的东西
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:onClick="click_screen" >

...

</RelativeLayout>

或者只是覆盖Activity中的onTouchEvent,就像这样.-

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        Intent click_screen=new Intent(this, MainActivity.class);
        startActivity(click_screen);
    }

    return super.onTouchEvent(event);
}

答案 1 :(得分:0)

Myabe你可以尝试这段代码,我制作textview.setClickable(true)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final TextView textView = (TextView)findViewById(R.id.textView1);
    textView.setClickable(true);
    textView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    startActivity(new Intent(this, MainActivity.class));
}

答案 2 :(得分:0)

您想在屏幕上的任何位置单击时移动到其他活动吗?那么为什么要在该文本视图上设置onclick?在顶部相对布局/线性布局上设置onclick属性而不是在textview上设置。

答案 3 :(得分:0)

您的代码是在MainActivity中,并且通过意图您再次尝试转到MainActivity.class?指定您要去的活动,例如:

Intent click_screen=new Intent(MainActivity.this, nextActivity.class);
    startActivity(click_screen);

答案 4 :(得分:0)

## Please add clickable property ##
 <TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Click Me"
    android:clickable="true" 
    android:onClick="click_screen" />