设置Android点击查看 [英] Set up android view clickable

查看:181
本文介绍了设置Android点击查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立这几启动第二陆续飞溅活动活动。无论如何,我想增加一个功能:启动次活动不仅经过5秒,但在我的观点泼点击后。
当我设置了下:

 视图V =(查看)findViewById(R.layout.splash);
    v.setOnClickListener(本);
    的setContentView(五);

而不是

 的setContentView(R.layout.splash);

我的项目将无法运行。
问题出在哪里?

下面是我的code:

 公共类SlashC扩展活动实现Runnable,OnClickListener {
螺纹定时器;
MediaPlayer的hTree;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    视图V =(查看)findViewById(R.layout.splash); //问题从这里开始
    v.setOnClickListener(本);
    的setContentView(五);    hTree = MediaPlayer.create(这一点,R.raw.happy_tree);
    hTree.start();
    定时器=新主题(本);
    timer.start();}
公共无效的onClick(视图v){
    // TODO自动生成方法存根
    意向类别2 =新意图(com.roger.calc.MainActivity);
    startActivity(等级2);
}
公共无效的run(){
    // TODO自动生成方法存根
    尝试{
        timer.sleep(5000);
    }赶上(InterruptedException的E){
        // TODO自动生成catch块
        e.printStackTrace();
    }最后{
        意向类别2 =新意图(com.roger.calc.MainActivity);
        startActivity(等级2);
    }
}@覆盖
保护无效的onPause(){
    // TODO自动生成方法存根
    super.onPause();
    hTree.release();
    完();
}}

和XML

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:背景=@绘制/ splash2
机器人:可点击=真正的>
< / LinearLayout中>


解决方案

设置布局属性使的LinearLayout点击的android:点击=真正的机器人:可聚焦=真正的的android:focusableInTouchMode =真正的从XML或 setClickable(真) 从code。设置onClickListener为

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /扑通
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:focusableInTouchMode =真
    机器人:可点击=真
    机器人:可聚焦=真
    机器人:方向=垂直
    机器人:背景=@绘制/ splash2/>

在code部分:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.splash);
    ((的LinearLayout)findViewById(R.id.splash))setOnClickListener(本);    ///您的code HERE

I set up splash activity which start another activity after few second. Anyway I wanted to add one more capability: to start the second activity not only after 5 second but after a click on my splash view. As soon as I set up the next:

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

instead of

setContentView(R.layout.splash);

my project does not run. Where is the problem?

Here is my code:

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();
}}

And 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>

解决方案

Make the LinearLayout clickable by setting layout attribute android:clickable="true", android:focusable="true" and android:focusableInTouchMode="true" from xml or setClickable(true) from code. set onClickListener as

<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"/>

and in code part:

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

    /// YOUR CODE HERE

这篇关于设置Android点击查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆