未在图像视图上调用setOnClickListener

时间:2015-12-05 17:09:11

标签: java android imageview onclicklistener

我想在下一个图像视图上调用一个片段。所以我在图像视图上放了OnClickListener,但它根本没有被调用..

可能是什么问题?

GoSend xml Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    android:id="@+id/LinearLayoutGoSend">
    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@android:color/white"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="15dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/DriversNear"
        android:id="@+id/textView10"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp" />


    <fragment android:layout_width="match_parent"
        android:layout_height="250dp"
        android:id="@+id/map"
        tools:context=".GoSend"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_marginTop="10dp" />

    </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_weight="1.00"
                android:background="@android:color/white"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="@string/From"
                    android:id="@+id/textView11"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp" />

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="08dp"
                    android:id="@+id/imageView11"
                    android:background="@drawable/line2"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:id="@+id/imageView10"
                        android:layout_marginLeft="20dp"
                        android:background="@drawable/ic_place_black_48dp"
                        android:layout_marginTop="05dp" />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/editText_from"
                        android:text="@string/Loc"
                        android:visibility="visible"
                        android:drawableRight="@drawable/ic_chevron_right_black_24dp"
                        android:layout_marginRight="10dp"
                        android:cursorVisible="false"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:id="@+id/imageView12"
                        android:layout_marginLeft="20dp"
                        android:background="@drawable/ic_description_black_48dp"
                        android:layout_marginTop="05dp" />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/editText_from_details"
                        android:layout_marginRight="10dp"
                        android:hint="Location details"
                        android:cursorVisible="true"/>


                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:id="@+id/textView12"
                    android:layout_marginLeft="48dp"
                    android:hint="House no./Floor/Landmark" />

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

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:id="@+id/imageView13"
                        android:layout_marginLeft="20dp"
                        android:background="@drawable/ic_person_black_48dp"
                        android:layout_marginTop="05dp"
                        />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/contactDetailsFrom"
                        android:layout_marginRight="10dp"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:drawableRight="@drawable/ic_expand_more_black_24dp"
                        android:text="@string/additionalContact"
                        android:layout_gravity="center_vertical"
                        android:layout_marginTop="05dp"
                        android:layout_marginLeft="05dp" />
                </LinearLayout>


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:id="@+id/textView13"
                    android:layout_marginLeft="48dp"
                    android:hint="Input if you're not sender" />

            </LinearLayout>


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:id="@+id/textView15"
                    android:layout_marginLeft="48dp"
                    android:hint="House no./Floor/Landmark" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:id="@+id/textView16"
                        android:layout_marginLeft="48dp"
                        android:hint="input if you're not receiver" />
                </LinearLayout>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:background="@android:color/white"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_weight="1.00">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="@string/Details"
                    android:id="@+id/textView17"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp"
                    />

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="08dp"
                    android:id="@+id/imageView16"
                    android:background="@drawable/line2"/>
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:id="@+id/imageView"
                        android:layout_marginLeft="20dp"
                        android:background="@drawable/ic_check_circle_black_48dp"
                        android:layout_marginTop="05dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="@string/pickup"
                        android:id="@+id/textView18"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="05dp" />

                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:id="@+id/imageView20"
                        android:layout_marginLeft="20dp"
                        android:background="@drawable/box5"
                        android:layout_marginTop="05dp" />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/editText_pac"
                        android:layout_marginRight="10dp"
                        android:hint="Items to deliver"/>

                </LinearLayout>
        </LinearLayout>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView_next"
                android:layout_gravity="center_horizontal"
                android:layout_weight="0.42"
                android:clickable="true"
                android:background="@drawable/ic_chevron_right_black_48dp" />
        </LinearLayout>

    </ScrollView>

</LinearLayout>

在GoSend活动中调用此内容

  next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            DetailsFragment fragment = new DetailsFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.LinearLayoutGoSend, fragment).commit();

        }
    });

GoSend活动

public class GoSend extends AppCompatActivity {

private GoogleMap mMap;
private MarkerOptions markerOptions;
private LinearLayout ll;
private TextView additionalContactFrom;
private TextView additionalContactTo;
private LinearLayout linearLayoutFrom;
private LinearLayout linearLayoutTo;
private ImageView next;

private Toolbar toolbar;
private EditText editTextLocation;
private EditText edtxt_from;
private EditText edtxt_to;
private String stringAddress;
String stringAddress1;
String stringAddressTo;
EditText locdetailsFrom;
EditText locdetailsTo;
private Intent i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gosendlayout);

    setUI();

    if (Build.VERSION.SDK_INT >= 21) {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

public void setUI() {

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("COURIER");
    setSupportActionBar(toolbar);
   getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    edtxt_from=(EditText)findViewById(R.id.editText_from);
    edtxt_to=(EditText)findViewById(R.id.editText_to);
    locdetailsFrom = (EditText) findViewById(R.id.editText_from_details);
    locdetailsTo = (EditText) findViewById(R.id.editText_to_details);
    additionalContactFrom = (TextView)findViewById(R.id.contactDetailsFrom);
    additionalContactTo = (TextView)findViewById(R.id.contactDetailsTo);
    linearLayoutFrom = (LinearLayout)findViewById(R.id.LinearLayoutFrom);
    linearLayoutTo = (LinearLayout)findViewById(R.id.LinearLayoutTo);
    next = (ImageView)findViewById(R.id.imageView_next);
    next.setClickable(true);

    try {
        if (mMap == null) {
            mMap = ((MapFragment) getFragmentManager().
                    findFragmentById(R.id.map)).getMap();
        }
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        mMap.setMyLocationEnabled(true);
    } catch (Exception e) {
        e.printStackTrace();
    }

    edtxt_from.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

             i=new Intent(getApplicationContext(),PickLocationActivity.class);
            startActivity(i);
        }
    });

    edtxt_to.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            i=new Intent(getApplicationContext(),PickLocationActivity.class);
            startActivity(i);
        }
    });


    additionalContactFrom.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(linearLayoutFrom.getVisibility() == View.GONE){

                linearLayoutFrom.setVisibility(View.VISIBLE);

            }else{
              linearLayoutFrom.setVisibility(View.GONE);

            }
        }
    });


    additionalContactTo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(linearLayoutTo.getVisibility() == View.GONE){

                linearLayoutTo.setVisibility(View.VISIBLE);

            }else{
              linearLayoutTo.setVisibility(View.GONE);

            }
        }
    });

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            DetailsFragment fragment = new DetailsFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.LinearLayoutGoSend, fragment).commit();

        }
    });
}

@Override
public void onResume() {
    super.onResume();  // Always call the superclass method first
    Bundle bundle = getIntent().getExtras();
    if(bundle != null)
        stringAddress  = bundle.getString("address");

}

}

DetailsFragment

public class DetailsFragment extends Fragment {
    private Toolbar toolbar;
    public DetailsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_details, container, false);

        return view;
    }

}

片段布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal|top"
    android:background="@android:color/transparent">
    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/white"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/Details"
            android:id="@+id/textView22"
            android:layout_gravity="center_vertical"
            android:layout_marginTop="15dp"
            android:layout_marginStart="15dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/white"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/payment"
            android:id="@+id/textView24"
            android:layout_marginStart="15dp"
            android:layout_marginTop="15dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="08dp"
            android:id="@+id/imageView23"
            android:background="@drawable/line2"
            android:layout_marginTop="10dp" />


        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@android:color/white"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:weightSum="1">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:id="@+id/imageView24"
                android:background="@drawable/coins49"
                android:layout_marginStart="10dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/price"
                android:id="@+id/textView25"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="10dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/textView26"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="190dp" />

            </LinearLayout>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="08dp"
                android:id="@+id/imageView26"
                android:background="@drawable/line2"
                android:layout_marginTop="05dp" />


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/imageView25"
            android:layout_marginStart="10dp"
            android:background="@drawable/currency13" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/total"
            android:id="@+id/textView27"
            android:layout_marginStart="10dp"
            android:layout_gravity="center_vertical" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/textView28"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="190dp" />

    </LinearLayout>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="08dp"
            android:id="@+id/imageView27"
            android:background="@drawable/line2"
            android:layout_marginTop="05dp" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@android:color/white"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:id="@+id/imageView28"
                android:layout_marginStart="10dp"
                android:background="@drawable/credit101" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/paywith"
                android:id="@+id/textView29"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="10dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/textView30"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="180dp" />
        </LinearLayout>

    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView29"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:background="@drawable/ic_chevron_right_black_48dp" />


</LinearLayout>

请帮助......

1 个答案:

答案 0 :(得分:0)

为什么使用以下代码行,请删除它们并检查效果: -

android:clickable="true"
next.setClickable(true);

它没有意识到工作,但它最大限度地工作。

如果您将视图设置为可点击,那么它将消耗点击而不会将其传播到容器。