我第一次在 fragment 中工作...在主要 Activity 中我有一个 spinnerListner 并且随着微调器中的项目更改,在 fragment 容器中... fragment 相应地更改,我刚刚在 fragment.xml 文件中创建了一些复选框.. .我只想在每次注册点击时为每个选中的复选框发布并存储一个不同的值“NameoftheCheckbox”,并为每个未选中的复选框(甚至来自容器中未显示的 fragment )发布另一个值“Null”。就像我试图在我的主要 Activity 中通过 RegisterUser 函数存储名称和引脚的方式一样......
我的主要 Activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_Name, et_Pin;
private Button RegisterBTN;
private ProgressDialog progressDialog;
private CheckBox checka, checkb;
private Spinner sDropdown;
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_Name = (EditText)findViewById(R.id.etName);
et_Pin = (EditText)findViewById(R.id.etPin);
checka = (CheckBox)findViewById(R.id.checkBox_a);
RegisterBTN = (Button)findViewById(R.id.btn_Reg);
progressDialog = new ProgressDialog(this);
//progressDialog.setMessage();
adapter = ArrayAdapter.createFromResource(this, R.array.spinner_options, android.R.layout.simple_spinner_item);
spinnerListner();
RegisterBTN.setOnClickListener(this);
}
public void spinnerListner(){
sDropdown = (Spinner)findViewById(R.id.spinner2);
sDropdown.setAdapter(adapter);
sDropdown.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
getSupportFragmentManager().beginTransaction().replace(R.id.frag, Info_frag.newInstance()).addToBackStack(null).commit();
break;
case 1:
getSupportFragmentManager().beginTransaction().replace(R.id.frag, Plumber_frag.newInstance()).addToBackStack(null).commit();
break;
case 2:
getSupportFragmentManager().beginTransaction().replace(R.id.frag, Painter_frag.newInstance()).addToBackStack(null).commit();
break;
case 3:
getSupportFragmentManager().beginTransaction().replace(R.id.frag, Electrician_frag.newInstance()).addToBackStack(null).commit();
//Toast.makeText(MainActivity.this,"F3", Toast.LENGTH_SHORT).show();
break;
}
/**
* TextView spinnerDailogText = (TextView)view;
Toast.makeText(MainActivity.this, "You selected:"+ spinnerDailogText.getText(), Toast.LENGTH_SHORT).show();*/
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
}
private void registerUser(){
final String name = et_Name.getText().toString().trim();
final String pin = et_Pin.getText().toString().trim();
progressDialog.setMessage("Registering please wait...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST,
Constants.REGISTER,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.hide();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("reg_name", name);
params.put("reg_pin", pin);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
public void onClick(View v) {
if (v == RegisterBTN);
registerUser();
}
}
我的 fragment Activity
public class Electrician_frag extends Fragment {
public static Electrician_frag newInstance() {
Electrician_frag fragment = new Electrician_frag();
return fragment;
}
public Electrician_frag(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.fragment_electrician, container, false);
return rootview;
}
}
另一个 fragment Activity
public class Painter_frag extends Fragment {
public static Painter_frag newInstance(){
Painter_frag fragment = new Painter_frag();
return fragment;
}
public Painter_frag(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.fragment_painter, container, false);
return rootview;
}
}
我的 PainterFragment.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<CheckBox
android:id="@+id/checkBox_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="124dp"
android:layout_marginStart="124dp"
android:text="@string/a"
android:textSize="20sp"
android:textStyle="bold" />
<CheckBox
android:id="@+id/checkBox_y"
style="@android:style/Widget.Holo.Light.CompoundButton.CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="70dp"
android:layout_marginStart="70dp"
android:layout_toEndOf="@+id/checkBox_x"
android:layout_toRightOf="@+id/checkBox_x"
android:text="@string/b"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
</FrameLayout>
最佳答案
据我了解你的问题。我们有以下场景。
1. 你有 MainActivity。
2. 在这个 MainActivity 中,您正在尝试实现一个注册表单。
3. 在注册表单中,您有多个包含复选框的 fragment 。
4.当你点击注册按钮(在MainActivity里面),你想发布一些取决于复选框是否被选中的数据。
如果是这种情况,您可以使用 Otto 事件总线库或 Guava 事件总线库轻松实现目标。这种方式还可以帮助您维护应用程序代码的模块化。
我正在提供一个简短的信息,您可以如何做到这一点。
请按照以下步骤获取解决方案。
compile 'com.squareup:otto:1.3.8'
public class Events {
public static class RegisterEvent {
public RegisterEvent() {}
}
}
public class GlobalBus {
private static Bus sBus;
public static Bus getBus(){
if (sBus == null)
sBus = new Bus();
return sBus;
}
}
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GlobalBus.getBus().post(new Events.RegisterEvent());
}
});
GlobalBus.getBus().register(this);
@Subscribe
public void showMessage(Events.RegisterEvent events) {
/// Here, you need to check whether checkboxes are checked or not. Send message to the server depending on that. Repeat same process for every checkbox.
}
注意:请记住,单击注册按钮时,每个 fragment 都会收到通知。然后,它会检查复选框(复选框是否被选中)并将请求发送到服务器。
注意:如果您想了解更多关于 otto 事件总线库的信息,您可以查看 https://tutorialwing.com/otto-event-bus-tutorial-example/
另外,如果你想使用 Guava eventbus,你可以查看 https://tutorialwing.com/android-eventbus-library-example/
这两个库都有一些优点和缺点。
关于javascript - 我在 fragment 中有一个复选框,我希望它在单击注册按钮时发布一些数据这个按钮在我的主要 Activity 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43042631/