我只是出于娱乐目的而编程,并尝试自学与Android Studios相关的Java。
因为我有一个应用程序抽屉,所以我需要碎片(这是我目前的知识状态)。
见照片click here
问题是我无法使用findViewById
从输入中获取数字。看照片
click here
但是我需要它们来进行计算。
您有建议吗? 您是否有示例,说明如何在一个片段中执行数学计算?
答案 0 :(得分:0)
您可以使用findViewById(),但需要获取视图对象
您需要像这样使用它:
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.testclassfragment, container, false);
EditText edittext1 = (EditText ) view.findViewById(R.id.edittext1);
EditText edittext2 = (EditText ) view.findViewById(R.id.edittext2);
EditText edittext3 = (EditText ) view.findViewById(R.id.edittext3);
TextView answer = (TextView ) view.findViewById(R.id.tv_ans);
int a=Integer.parseInt(edittext1.getText());
int b=Integer.parseInt(edittext2.getText());
int c=Integer.parseInt(edittext3.getText());
int ans=a+b+c; //for example
answer.setText(""+ans);
return view;
}