简体   繁体   English

使用LinearLayout,ImageView,TextView设置onClickListener

[英]set onClickListener with LinearLayout, ImageView, TextView

I have a Linear Layout with ImageView (icon) and TextView ("Settings") inside. 我有一个线性布局,其中包含ImageView(图标)和TextView(“设置”)。 Like this : 像这样 :

在此处输入图片说明

I would like when the user clicks on the LinearLayout or ImageView or TextView, another Actitivy is started. 我想当用户单击LinearLayout或ImageView或TextView时,启动另一个Actitivy。

So I do this in the code: 所以我在代码中这样做:

OnClickListenerLessons mOnClickListener = new mOnClickListenerLessons(){
@Override
            public void onClick(View v) {
                Intent i = new Intent(getActivity(), nextActivity.class);
                startActivity(i);
            }}

imageView.setOnClickListener(mOnClickListener);
linearLayout.setOnClickListener(mOnClickListener);
textView.setOnClickListener(mOnClickListener);

And I found that it is quite bulky and messy, is there anyway to make the code cleaner? 而且我发现它非常笨拙且杂乱无章,是否可以使代码更整洁?

Many thanks! 非常感谢!

PS: here is my xml file PS:这是我的xml文件

 <LinearLayout>
     ...
      <LinearLayout
            android:id="@+id/linear_layout_wrapper_lessons"
            style="@style/width_height_margin_for_items"
            android:layout_weight="25"
            android:clickable="true"
            android:gravity="center"
            android:orientation="horizontal" >
            <ImageView
                android:id="@+id/lessons_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/puzzle_piece" />

            <TextView
                android:id="@+id/home_lesson_textView"
                style="@style/text_view"
                android:clickable="true"
                android:text="@string/home_lesson_button" />
        </LinearLayout>
    ...
</LinearLayout>

here is the style.xml for text_view 这是text_view的style.xml

<style name="text_view">
    <item name="android:background">@null</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:textSize">22sp</item>
    <item name="android:layout_margin">5dp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">@color/home_buttons_select_state_color</item>
</style>

When you set an onClickListener to TextView or ImageView , you should also declare those as android:clickable="true" or setClickable(true) . onClickListener设置为TextViewImageView ,还应将其声明为android:clickable="true"setClickable(true)

Also having a LinearLayout with an ImageView and a TextView might not be necesseary, why don't you just add the image with android:drawableLeft ? 同样也不需要带有ImageViewTextViewLinearLayout ,为什么不只使用android:drawableLeft添加图像呢? Nesting LinearLayouts is a very bad habit. 嵌套LinearLayouts是一个非常不好的习惯。

Implement OnClickListener in your activity and override the onClick() method and write in this manner 在您的活动中实现OnClickListener并重写onClick()方法并以这种方式编写

@Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.imageView1:
        case R.id.textview1:
        case R.id.linearlayout1:
            Intent i = new Intent(getActivity(), nextActivity.class);
            startActivity(i);
            break;
        default:
            break;
        }

    }

You can have a case of linearlayout only because your controls are within it 您只能使用linearlayout的情况是因为您的控件在其中

If you have the TextView and ImageView inside the LinearLayout, is useless to set the click listener on them if you don't need to handle different actions of the click listener of the parent. 如果您在LinearLayout中具有TextView和ImageView,则在不需要处理父级Click侦听器的其他操作时,就无法在其上设置Click侦听器。

You just need to do: 您只需要做:

linearLayout.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        Intent i = new Intent(getActivity(), nextActivity.class);
        startActivity(i);
    }}
});

Arrange components in layout XML as bellow 按如下所示在布局XML中排列组件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Image android:="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
</LinearLayout>

Then add click listener for LinearLayout 然后为LinearLayout添加点击监听器

linearLayout.setOnClickListener(mOnClickListener);

You can use single button with image and text.then set button width and height match_parent.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/ButtonTest"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawableLeft="@drawable/ic_launcher"
    android:paddingTop="32sp"
    android:text="this is text"
    android:textColor="#FFFFFF" >
</Button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM