吐司在Fragemet里面不起作用

时间:2014-03-13 05:23:56

标签: android android-fragments toast

我有一个片段,MainActivity.Fragment在Main Activity.in片段内调用我放Toast(在ImageView的测试目的中)。吐司没有工作。请在我使用的代码下找到。

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

                }

        }

    };

}

3 个答案:

答案 0 :(得分:3)

试试这个..

v == loginbuttton更改为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();
        }
    }

修改

@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;
}

答案 1 :(得分:1)

==比较对象引用,它检查两个操作数是否指向同一个对象。

在这一行

if(v==loginbuttton)

改为

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

答案 2 :(得分:0)

这个答案可以帮到你:)

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

}

}