Вопрос

i have one fragment and MainActivity.Fragment is called inside the Main Activity.in fragment i put Toast (in test purpose of ImageView).the toast there isn't working.please find below the code i used.

   package com.maintab;
import com.example.tesfragement.R;
import com.example.tesfragement.R.layout;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;


public class Login extends Fragment   {

    View view;
    ImageView loginbuttton,logoutbutton;


    public Login() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.login, container, false);
        loginbuttton=(ImageView) view.findViewById(R.id.loinbutton);
        loginbuttton.setOnClickListener( btnClick);

        return view;
    }

    ImageView.OnClickListener btnClick=new ImageView.OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             if((v.getId()==loginbuttton.getId()))
                {
                    Toast.makeText(getActivity(),"hello", Toast.LENGTH_LONG).show();

                }

        }

    };

}
Это было полезно?

Решение

Try this..

Change v == loginbuttton to v.getId() == R.id.loinbutton

   @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId() == R.id.loinbutton)
        {
            Toast.makeText(getActivity(),"hello", Toast.LENGTH_LONG).show();
        }
    }

EDIT:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view= inflater.inflate(R.layout.login, container, false);
    loginbuttton=(ImageView) view.findViewById(R.id.loinbutton);
    loginbuttton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(),"hello", Toast.LENGTH_LONG).show();
        }

    });

    return view;
}

Другие советы

== compares object references, it checks to see if the two operands point to the same object.

In this line

if(v==loginbuttton)

change into

if(v.getId()==loginbuttton.getId())

this answer may helps you :)

public class Login extends Fragment implements OnClickListener {


View view;
private ImageView loginbuttton;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.login, container, false);
    loginbuttton = (ImageView) view.findViewById(R.id.loginbutton);

    return view;
}

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

    loginbuttton.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.loginbutton:
        Toast.makeText(getActivity(), "login", Toast.LENGTH_LONG).show();
        break;

      //    case R.id.logoutbutton:
      //            Toast.makeText(getActivity(), "logout", Toast.LENGTH_LONG).show();
      //    break;

    default:
        break;
    }

}

}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top