【问题标题】:Android - TextView onClick Layout onTouchAndroid - TextView onClick 布局 onTouch
【发布时间】:2013-08-15 23:50:19
【问题描述】:

我有一个小问题,在我的 xml 中有这个:

<LinearLayout
        android:id="@+id/mainView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:gravity="center"
            android:text="@string/app_name"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="18sp"
            android:layout_margin="10dp" />

        <LinearLayout
            android:id="@+id/fistItem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="@string/app_name"
                android:textColor="#fff"
                android:textSize="20sp" />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/secondItem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="@string/app_name"
                android:textColor="#fff"
                android:textSize="20sp" />
        </LinearLayout>
</LinearLayout>

我的活动文件中有这个:

public class MyActivity extends Activity implements OnTouchListener,
        OnClickListener {

    LinearLayout mainView;
    LinearLayout firstItem;
    TextView header;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);

        mainView = (LinearLayout) findViewById(R.id.mainView);
        firstItem = (LinearLayout) findViewById(R.id.fistItem);
        header = (TextView) findViewById(R.id.title);
        mainView.setOnTouchListener(this);
        firstItem.setOnClickListener(this);

    }

    int n = 0;

    @Override
    public void onClick(View v) {
        header.setText("First Item Clicked: " + ++n + " times");
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Toast.makeText(getApplicationContext(), "View Touched",
                    Toast.LENGTH_SHORT).show();
        }
        return false;
    }
}

当我单击 firstItem 时出现问题。我可以看到标题文本如何变化,但 Toast 没有弹出,但是当我单击 secondItem 时它会弹出。

【问题讨论】:

  • 第二项是什么?

标签: android onclick touch-event


【解决方案1】:

为了通过 ma​​inView 上的 onTouchfirstItem 引发 onClick 事件,您需要在 onTouch 方法中返回 true。例如:

@Override
public boolean onTouch(View v, MotionEvent event) {
    ...

    return true;  //now it will raise any registered onClick event too
}

当您在onTouch 中返回false 时,它会消耗触摸事件并且不会将其传递给后续的onClick 事件。

【讨论】:

  • 实际上,当我单击 firstItem 时会调用 onClick,但不会调用 mainViewonTouch 侦听器。我认为如果它是相反的会起作用
【解决方案2】:

你错过了这个

        firstItem.setOnTouchListener(this);

在你的 onCreate() 中

【讨论】:

猜你喜欢
  • 2016-07-28
  • 1970-01-01
  • 2016-08-07
  • 2012-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多