请问有人可以帮我一下吗?我已经为这个问题苦苦挣扎了几个小时。我有一个 Main 和 Start Activity ,Main 是带有一堆信息(TextView)的启动器,一旦有人触摸屏幕,它应该立即转换为 Start Activity 。我找到了一些有关如何执行此操作的堆栈溢出信息(在相对布局上设置 onTouchListener()),但它对我不起作用,所以每次我尝试使用:
RelativeLayout layout= (RelativeLayout)findViewById(R.id.relativeLayout1);
layout.setOnTouchListener(this);
它告诉我将“this”转换为“OnTouchListener”,结果如下:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView display1;
TextView display2;
TextView display3;
TextView display4;
ImageView image1;
RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_screen);
getActionBar().setTitle("AutoSMS");
RelativeLayout layout= (RelativeLayout)findViewById(R.id.relativeLayout1);
layout.setOnTouchListener((OnTouchListener) this);
display1 = (TextView) findViewById(R.id.textView1);
display2 = (TextView) findViewById(R.id.textView2);
display3 = (TextView) findViewById(R.id.textView3);
display4 = (TextView) findViewById(R.id.textView4);
image1 = (ImageView) findViewById(R.id.imageView2);
Intent openStarting = new Intent("android.intent.action.MAIN");
startActivity(openStarting);
}
}
这是 list ,以防万一
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.autosms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Start"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".End"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.END" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
DDMS 给我 ClassCastException,MainActivity 无法转换为 ViewOnTouchListener
任何帮助将不胜感激!
最佳答案
您可以将整个 Activity 实现为 onTouchListener
或创建一个新的 onTouchListener
当你添加它时。喜欢
public class myActivity extends Activity implements onTouchListener
或
thisObject.add(new onTouchListener(){
//TODO: Add code
});
block 引用>如果您的 Activity 不是 onTouchListener,则无需将其强制转换为 onTouchListener。强制转换是指如果可能的话,您会获得某物的特定值,就像您有
double
一样。你想得到它的值int
。您只需创建一个新的onTouchListener
或者将您的 Activity 变成一项 Activity ,因为您没有任何 Activity 。
关于java - 触摸主 Activity 中的任何位置时无法启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27831954/