onClickListener 在片段中不起作用 [英] onClickListener does not work in fragment

查看:25
本文介绍了onClickListener 在片段中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

片段中的 onClicklistener 出现了一些问题.如果我点击按钮,什么都不会发生.我既没有从 Logcat 中的 onClicklistener 收到消息,也没有在屏幕上显示 Toast,但是我在代码中找不到错误.有什么想法吗?

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

}
}    

推荐答案

我认为问题出在你的代码中

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

返回你已经膨胀的视图

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

     return inputFragmentView;
    }

这篇关于onClickListener 在片段中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆