【问题标题】:Set up android view clickable设置android view可点击
【发布时间】:2012-07-28 12:21:01
【问题描述】:

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

【问题讨论】:

    标签: android view clickable


    【解决方案1】:

    通过在 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
    

    【讨论】:

    • 我还在我的线程中添加了一个 if 语句。否则第二个活动加载两次。
    • 你在上面的xml中有两次android:clickable="true"
    • 为什么我们需要将某些东西设置为可点击?当我们添加一个监听器时,它肯定可以点击吗?
    【解决方案2】:

    可能你的 xml 布局中缺少这个

    android:id="@+id/splash"
    

    【讨论】:

      【解决方案3】:

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

      线性布局 v = (LinearLayout)findViewById(R.layout.splash);

      v.setOnClickListener(this);

      【讨论】:

      • Rajesh 使用Linearlayout v = (LinearLayout)findViewById(R.id.splash); 而不是Linearlayout v = (LinearLayout)findViewById(R.layout.splash);
      • 谢谢各位!这正是我所需要的。
      【解决方案4】:

      您不能在setContentView() 之前使用findViewById(int id),布局充气器也将有助于在布局上设置android:clickable="true"android:focusable="true"android:focusableInTouchMode="true"

      【讨论】:

        猜你喜欢
        • 2020-01-17
        • 2012-04-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-23
        • 1970-01-01
        • 2019-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多