我有一个简单的应用程序,它有1个Edittext和1个Button.My应用程序获取前两个Edit文本中的数字,当按下按钮时,最后一个EditText显示前两个EditText.Until的总和,我已经完成了这一切都与Activity有关,但是我想用fragment实现这个。我有this源代码,有导航抽屉,当尝试在这段代码中做同样的事情我有setOnClicklistener的问题
这是我的代码:
package info.androidhive.slidingmenu;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class FindPeopleFragment extends Fragment implements OnClickListener {
EditText Num1, Num2, Eq;
Button Sum;
int S;
public FindPeopleFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View InputFragmentView = inflater.inflate(R.layout.fragment_find_people, container, false);
Num1 = (EditText) InputFragmentView.findViewById(R.id.Num1);
Num2 = (EditText) InputFragmentView.findViewById(R.id.Num2);
Eq = (EditText) InputFragmentView.findViewById(R.id.Eq);
Sum = (Button) InputFragmentView.findViewById(R.id.Sum);
Sum.setOnClickListener(new OnClickListener(){
public void onClick(View view)
{
String N1 = Num1.getText().toString();
int N11 = Integer.parseInt(N1);
String N2 = Num2.getText().toString();
int N22 = Integer.parseInt(N2);
S = N11 + N22;
Eq.setText("" + S);
}
});
return InputFragmentView;
}
}
答案 0 :(得分:3)
package info.androidhive.slidingmenu;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class FindPeopleFragment extends Fragment {
EditText Num1, Num2, Eq;
Button Sum;
int S;
public FindPeopleFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View InputFragmentView = inflater.inflate(
R.layout.fragment_find_people, container, false);
Num1 = (EditText) InputFragmentView.findViewById(R.id.Num1);
Num2 = (EditText) InputFragmentView.findViewById(R.id.Num2);
Eq = (EditText) InputFragmentView.findViewById(R.id.Eq);
Sum = (Button) InputFragmentView.findViewById(R.id.Sum);
;
Sum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String N1 = Num1.getText().toString();
int N11 = Integer.parseInt(N1);
String N2 = Num2.getText().toString();
int N22 = Integer.parseInt(N2);
S = N11 + N22;
Eq.setText("" + S);
Toast.makeText(getActivity(), "your Sum is : " + S,
Toast.LENGTH_LONG).show();
}
});
return InputFragmentView;
}
}
答案 1 :(得分:1)
以这种方式尝试:
Sum = (Button) InputFragmentView.findViewById(R.id.Sum);
Sum.setOnClickListener(this);
return InputFragmentView;
}
public void onClick(View v) {
switch (v.getId()){
case R.id.Sum:
String N1 = Num1.getText().toString();
int N11 = Integer.parseInt(N1);
String N2 = Num2.getText().toString();
int N22 = Integer.parseInt(N2);
S = N11 + N22;
Eq.setText("" + S);
break;
}
}
无论如何,发布您的Logcat输出以了解您面临的问题
答案 2 :(得分:0)
或者只是保持这样,但删除OnClickListener的实现。 例如: public class FindPeopleFragment extends Fragment实现OnClickListener { ... }