onClickListener不在片段工作 [英] onClickListener does not work in fragment
本文介绍了onClickListener不在片段工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些问题onClicklistener在片段。如果我按一下按钮没有任何反应都没有。我既得不到来自onClicklistener在logcat的消息,也不敬酒出现在屏幕上,但我找不到在code中的错误。任何想法?
我想AP preciate任何帮助!多谢!很遗憾我的英语不好
进口android.app.Fragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup;
进口android.widget.EditText;
进口android.widget.Button;
进口android.widget.Toast;
进口android.util.Log;
公共类InputFragment扩展片段
{
的EditText input_text;
文本字符串;
按钮translate_button;
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
{
查看InputFragmentView = inflater.inflate(R.layout.input_fgmt,集装箱,假);
input_text =(EditText上)InputFragmentView.findViewById(R.id.input_field);
translate_button =(按钮)InputFragmentView.findViewById(R.id.translate);
translate_button.setOnClickListener(新OnClickListener()
{
@覆盖
公共无效的onClick(视图查看)
{
Log.d(测试,onClickListener IST gestartet);
Toast.makeText(getActivity()getApplicationContext(),测试,Toast.LENGTH_LONG。).show();
saveInString();
}
});
返回inflater.inflate(R.layout.input_fgmt,集装箱,假);
}
公共无效saveInString()
{
如果(text.equals(空))
{
Toast.makeText(getActivity()getApplicationContext(),达斯费尔德北京时间秋波!,Toast.LENGTH_SHORT。).show();
}
其他
{
Toast.makeText(getActivity()getApplicationContext(),Speichern ...,Toast.LENGTH_LONG。).show();
。文字= input_text.getText()的toString();
Toast.makeText(getActivity()getApplicationContext(),Fertig,Toast.LENGTH_SHORT。).show();
}
}
}
解决方案
我觉得问题是在你这里code
公开查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
{
.....
....
//问题是在这里下面一行
返回inflater.inflate(R.layout.input_fgmt,集装箱,假);
}
返回您已经膨胀的看法
公开查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
{
.....
....
返回InputFragmentView;
}
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);
}
return your already inflated view
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
.....
....
return InputFragmentView;
}
这篇关于onClickListener不在片段工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文