我在片段中的onClicklistener遇到了一些问题。如果我点击按钮,根本没有任何事情发生。我既没有得到Logcat中的onClicklistener的消息,也没有在屏幕上显示Toast,但我在代码中找不到错误。有什么想法吗?
我很感激任何帮助!非常感谢!抱歉我的英语不好
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();
}
}
}
答案 0 :(得分:27)
我认为问题出现在你的代码中
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;
}
答案 1 :(得分:3)
更改
return inflater.inflate(R.layout.input_fgmt, container, false);
到
return InputFragmentView ;
同样改变:
translate_button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Log.d("Test", "onClickListener ist gestartet");
Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
saveInString();
}
});
并导入为导入android.view.View;
答案 2 :(得分:2)
对您的代码进行此更改
关于类定义:
public class InputFragment extends Fragment implements View.OnClickListener
调用setOnClickListener:
translate_button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View InputFragmentView)
{
Log.d("Test", "onClickListener ist gestartet");
Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
saveInString();
}
});
答案 3 :(得分:2)
创建所有视图的最佳方法是重新定义方法:
@Override
public void onActivityCreated(Bundle saved) {
super.onActivityCreated(saved);
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();
}
});
因为在创建了所有视图后调用了这个方法。 你应该阅读片段的生命周期。
答案 4 :(得分:0)
setOnItemClickListener
与我一起工作,而不是setOnClickListener