设置android视图可点击

设置android视图可点击,android,view,clickable,Android,View,Clickable,我设置了splash活动,几秒钟后开始另一个活动。无论如何,我想再增加一个功能:不仅在5秒钟之后,而且在单击我的启动视图之后,启动第二个活动。 一旦我设置了下一个: View v = (View)findViewById(R.layout.splash); v.setOnClickListener(this); setContentView(v); 而不是 setContentView(R.layout.splash); 我的项目没有运行。 问题在哪里 这是我的密码: pu

我设置了splash活动,几秒钟后开始另一个活动。无论如何,我想再增加一个功能:不仅在5秒钟之后,而且在单击我的启动视图之后,启动第二个活动。 一旦我设置了下一个:

View v = (View)findViewById(R.layout.splash); 
    v.setOnClickListener(this);
    setContentView(v);
而不是

setContentView(R.layout.splash);
我的项目没有运行。 问题在哪里

这是我的密码:

public class SlashC extends Activity implements Runnable, OnClickListener {
Thread timer;
MediaPlayer hTree;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View v = (View)findViewById(R.layout.splash); // problems start here
    v.setOnClickListener(this);
    setContentView(v);

    hTree = MediaPlayer.create(this, R.raw.happy_tree);
    hTree.start();
    timer = new Thread(this);
    timer.start();

}
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent class2 = new Intent("com.roger.calc.MainActivity");
    startActivity(class2);
}
public void run() {
    // TODO Auto-generated method stub
    try {
        timer.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        Intent class2 = new Intent("com.roger.calc.MainActivity");
        startActivity(class2);
    }       
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    hTree.release();
    finish();
}}
和XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/splash2"
android:clickable="true" >
</LinearLayout>

可能是因为您的xml布局中缺少此选项

android:id="@+id/splash"

可能您的xml布局中缺少了这一点

android:id="@+id/splash"
XML


setContentView(R.layout.name)

Linearlayout v=(Linearlayout)findViewById(R.layout.splash)

v、 setOnClickListener(此)

XML


setContentView(R.layout.name)

Linearlayout v=(Linearlayout)findViewById(R.layout.splash)


v、 setOnClickListener(此)

通过从xml或代码中设置布局属性,使线性布局可点击。将onClickListener设置为

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/splash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    android:clickable="true"
    android:focusable="true" 
    android:orientation="vertical"
    android:background="@drawable/splash2"/>

通过从xml或代码中设置布局属性,使线性布局可单击。将onClickListener设置为

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/splash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    android:clickable="true"
    android:focusable="true" 
    android:orientation="vertical"
    android:background="@drawable/splash2"/>

setContentView()
之前,您不能使用
findViewById(int-id)
布局充气器也可以帮助设置
android:clickable=“true”
android:focusable=“true”
android:focusableInTouchMode=“true”
,您不能使用
findViewById(int-id)
setContentView()之前,
布局充气器也可以帮助设置
android:clickable=“true”
android:focusable=“true”
android:focusableInTouchMode=“true”

Rajesh使用
Linearlayout v=(Linearlayout)findViewById(R.id.splash)而不是
Linearlayout v=(Linearlayout)findViewById(R.layout.splash)谢谢你!这就是我需要的。Rajesh使用
Linearlayout v=(Linearlayout)findViewById(R.id.splash)而不是
Linearlayout v=(Linearlayout)findViewById(R.layout.splash)谢谢你!这就是我需要的。我还在我的线程中添加了一个if语句。否则,第二个活动将加载两次。您在上面的xml中有两次android:clickable=“true”为什么我们需要将某些内容设置为clickable?当然,当我们添加一个侦听器时,它会使其可点击吗?我还在我的线程中添加了一个if语句。否则,第二个活动将加载两次。您在上面的xml中有两次android:clickable=“true”为什么我们需要将某些内容设置为clickable?当然,当我们添加一个监听器时,它就可以点击了?