设置android视图可点击

时间:2012-07-28 12:21:01

标签: android view clickable

我设置启动活动,在几秒钟后启动另一个活动。无论如何,我想再添加一个功能:不仅在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>

4 个答案:

答案 0 :(得分:7)

通过设置xml中的布局属性android:clickable="true"android:focusable="true"android:focusableInTouchMode="true"或代码中的setClickable(true),可以点击LinearLayout。将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"/>

并在代码部分:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    ((LinearLayout)findViewById(R.id.splash)).setOnClickListener(this);

    /// YOUR CODE HERE

答案 1 :(得分:1)

可能是你在xml布局中遗漏了这个

android:id="@+id/splash"

答案 2 :(得分:0)

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:id="@+id/splash"
android:clickable="true" >
</LinearLayout>

的setContentView(R.layout.name);

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

v.setOnClickListener(本);

答案 3 :(得分:0)

您无法在findViewById(int id) setContentView()之前使用android:clickable="true"布局inflater帮助您在布局上设置android:focusable="true"android:focusableInTouchMode="true"Event