Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

android - onClickListener does not work in fragment

I've got some problems with the onClicklistener in the fragment. If I click on the button nothing happens at all. I get neither a message from the onClicklistener in Logcat nor does a Toast appear on the screen, but I can't find the error in the code. Any ideas?

I'd appreciate any help! Thanks a lot! And sorry for my bad english

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import android.util.Log;

public class InputFragment extends Fragment
{   
EditText input_text;
String text;
Button translate_button;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{ 
    View InputFragmentView = inflater.inflate(R.layout.input_fgmt, container, false);

    input_text = (EditText) InputFragmentView.findViewById(R.id.input_field);

    translate_button = (Button) InputFragmentView.findViewById(R.id.translate);


    translate_button.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            Log.d("Test", "onClickListener ist gestartet");
            Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
            saveInString();

        }
    });

    return inflater.inflate(R.layout.input_fgmt, container, false);
}

public void saveInString()
{
    if(text.equals(null))
    {
        Toast.makeText(getActivity().getApplicationContext(), "Das Feld ist leer!", Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(getActivity().getApplicationContext(), "Speichern...", Toast.LENGTH_LONG).show();
        text = input_text.getText().toString();
        Toast.makeText(getActivity().getApplicationContext(), "Fertig", Toast.LENGTH_SHORT).show();
    }

}
}    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think problem is here in your code

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{ 
.....
....
 //problem is here below line
 return inflater.inflate(R.layout.input_fgmt, container, false);
}

return your already inflated view

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    { 
        View inputFragmentView = inflater.inflate(R.layout.input_fgmt, container, false);
    .....
    ....

     return inputFragmentView;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...