android - 设置android View 可点击

标签 android view clickable

我设置了启动 Activity ,几秒钟后开始另一个 Activity 。无论如何,我想再添加一项功能:不仅在 5 秒后启动第二个 Activity ,而且在单击启动 View 后启动第二个 Activity。 一旦我设置了下一个:

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>

最佳答案

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

关于android - 设置android View 可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11700975/

相关文章:

android - ShareActionProvider 在 android 中没有任何操作栏

java - 布局底部有两个按钮的 ScrollView

wpf - 为什么每次创建新 View 模型时都不会调用 View 构造函数?

安卓 : Clickable link of sdcard storage

javascript - Chrome 中可点击的圆形 Canvas Angular

android - 如何使用 Android NDK 构建主机静态库?

android - phonegap - navigator.connection.type 未定义

ios - 当viewWillAppear(再次)时重新初始化条形码阅读器

css - 减少窗口缩小时减去的距离 % 仅 CSS

hyperlink - JetBrains IDE 使 .txt 文件中的链接可点击