安卓按钮在android片段中不可见

时间:2015-12-16 12:14:20

标签: android json android-fragments

我使用json获取数据并设置我的微调器,我也想在我的操作员微调器中添加单选按钮但是单选按钮不可见而且不工作我想要可见按钮一些ID但是它不可见。

MyXML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"

>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/rount_corner1"
    android:layout_margin="@dimen/activity_horizontal_margin"

    >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:layout_marginTop="25dp"
        android:inputType="number"
        android:maxLength="10"
        android:textColor="#000000"
        android:layout_marginRight="40dp"
        android:hint="Enter Mobile number"
        android:textColorHint="#000000"
        android:layout_marginLeft="40dp"
        android:clickable="true"
        android:id="@+id/prenumber"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:layout_marginTop="7dp"
        android:layout_marginRight="40dp"
        android:layout_marginLeft="40dp"
        android:clickable="true"
        android:hint="Enter Recharge Amount"
        android:textColorHint="#000000"
        android:textColor="#000000"
        android:maxLength="4"
        android:inputType="number"
        android:id="@+id/rechergpre"
        />
    <Spinner
        android:id="@+id/operator_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:gravity="left"
        android:layout_marginRight="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="7dp"
        android:prompt="@string/appbar_scrolling_view_behavior">
                   </Spinner>
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="38dp"
        android:visibility="gone"
        >

        <RadioButton
            android:id="@+id/_TopUp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="TopUp "
            />

        <RadioButton
            android:id="@+id/Special"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Special(2G,3G,SMS,VOICE )"

            />

    </RadioGroup>
    <Button
        android:layout_width="match_parent"
        android:id="@+id/prepaid_submit"
        android:layout_height="wrap_content"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:text="Submit"
        android:textStyle="bold"
        android:layout_marginTop="60dp"
        />
  </LinearLayout>
</RelativeLayout>

片段中的Mycode

 private class DownloadJSON extends AsyncTask<Void, Void, Void> {
    MyApplication myOpt = (MyApplication)getActivity().getApplicationContext();
    protected Void doInBackground(Void... params) {
        json_data = new ArrayList<Json_Data>();
        datalist = new ArrayList<String>();
        oprList = new ArrayList<String>();
        jsonobject = JSONfunctions
                .getJSONfromURL("http://www.example.com");
        Log.d("Response: ", "> " + jsonobject);
        try {
            jsonarray = jsonobject.getJSONArray("data");
            for (int i = 0; i < jsonarray.length(); i++) {
                jsonobject = jsonarray.getJSONObject(i);
                Json_Data opt_code = new Json_Data();
                opt_code.setName(jsonobject.optString("name"));
                opt_code.setId(jsonobject.optString("ID"));
                json_data.add(opt_code);
                datalist.add(jsonobject.optString("name"));
                oprList.add(jsonobject.getString("ID"));
            }
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

protected void onPostExecute(Void args) {
        final Spinner mySpinner = (Spinner)getView().findViewById(R.id.operator_spinner);
        mySpinner
                .setAdapter(new ArrayAdapter<String>(getActivity(),
                        android.R.layout.simple_spinner_dropdown_item,
                        datalist));
        mySpinner
                .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    public void onItemSelected(AdapterView<?> arg0,
                                               View arg1, int position, long arg3) {
                      // get Operaotor cod in opt_code field

                        String opt_code = oprList.get(position);
                        String selectedItem = arg0.getItemAtPosition(position).toString();
                            Log.d("Selected operator is==", "======>" + selectedItem);
                            Log.d("Selected Value is======", "========>" + position);
                            Log.d("Selected ID is======", "========>" + opt_code);
                        if(opt_code=="8" || opt_code=="14"|| opt_code=="35"||opt_code=="36"||opt_code=="41"||opt_code=="43")
                        {
                            _RadioGroup = (RadioGroup)getView().findViewById(R.id.radioGroup);
                            _RadioGroup.setVisibility(View.VISIBLE);
                            mySpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                                    int selectedId = _RadioGroup.getCheckedRadioButtonId();

                                    // find the radiobutton by returned id
                                    RadioButton _RadioSex = (RadioButton)getView().findViewById(selectedId);

                                    Toast.makeText(getActivity(),
                                            _RadioSex.getText(), Toast.LENGTH_SHORT).show();

                                }
                            });

               }
                            String user1 = myOpt.setOperator(opt_code);
                            String opt_name = myOpt.setOpt_provider(selectedItem);


                    }
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub
                    }
                });
    }

图片如:

enter image description here

请帮帮我,谢谢。

1 个答案:

答案 0 :(得分:0)

像这样的chenge xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Spinner
    android:id="@+id/operator_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp"
    android:layout_marginTop="7dp"
    android:gravity="left"
    android:textAlignment="center" >
</Spinner>

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    android:paddingLeft="38dp"
    android:visibility="gone" >

    <RadioButton
        android:id="@+id/_TopUp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TopUp " />

    <RadioButton
        android:id="@+id/Special"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Special(2G,3G,SMS,VOICE )" />
</RadioGroup>

</LinearLayout>