我是新手,这是我的第一个程序。我正在尝试为自己创建一个账单程序。它有3个标签。在第二个标签中,用户将输入帐单名称,帐单金额和到期日期。那里有一个按钮来提交输入的信息,它将显示在选项卡3中。我正在尝试为按钮提交编写代码
package com.example.pking.mybudget;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class enterFragment extends Fragment {
EditText etBillName, etDate, etAmount;
Button btnEnter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_enter, container, false);
etBillName = (EditText) findViewById(R.id.etBillName);
etDate = (EditText) findViewById(R.id.etDate);
etAmount = (EditText) findViewById(R.id.etAmount);
btnEnter = (Button) findViewById(R.id.btnEnter);
btnEnter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String bill = etBillName.getText().toString();
String date = etDate.getText().toString();
String amount = etAmount.getText().toString();
}
});
}
}
take the data from tab 2 that was entered and display it in tab 3 using a for loop
答案 0 :(得分:0)
应用这些更改,然后重试。
创建一个变量并为其分配展开视图。
View rootView = inflater.inflate(R.layout.fragment_enter, container, false);
如果etBillName之类的变量和其他变量位于 fragment_enter 片段内,则使用该视图初始化变量
etBillName = (EditText) rootView.findViewById(R.id.etBillName);
onCreateView方法的结尾,**return rootView**
答案 1 :(得分:0)
Paul。我想我知道为什么您在调用findViewById()方法时会出错,这是因为您需要使用View对象才能访问小部件,并且返回视图行应该是您的最后一行onCreateView()方法中的代码。尝试以下操作:
package com.example.pking.mybudget;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class enterFragment extends Fragment {
EditText etBillName, etDate, etAmount;
Button btnEnter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myFragment = inflater.inflate(R.layout.fragment_enter, container, false);
etBillName = (EditText)myFragment.findViewById(R.id.etBillName);
etDate = (EditText)myFragment.findViewById(R.id.etDate);
etAmount = (EditText)myFragment.findViewById(R.id.etAmount);
btnEnter = (Button)myFragment.findViewById(R.id.btnEnter);
btnEnter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String bill = etBillName.getText().toString();
String date = etDate.getText().toString();
String amount = etAmount.getText().toString();
}
});
return myFragment;
}
}*emphasized text*
您可以使用SharedPreferences保存原始数据,然后可以在第3个选项卡中轻松显示它们。希望您发现我的答案有用。