2016-04-03 88 views
0

所以我創建的自定義xml對話,我想它喜歡這個節目:它有兩個RadioButton S和如果被點擊一些UI元素,如TextView S和EditTex TS出現,他們如果其他RadioButton被選中,則消失。單選按鈕和按鍵自定義沒有做任何事情對話框

問題是,ButtonRadioButton s沒有做任何事情。我已經設置了UI元素的可見性,因爲RadioButton被選中,但它不起作用。與Button一樣,它應該將結果傳遞給EditText,但按下時它什麼也不做。

我沒有得到任何錯誤,所以請不要讓我發佈logcats,因爲應用程序不會崩潰。

下面是對話的代碼:

statistikeInputDialogKalkulatoriFFMI = (ImageView) findViewById(R.id.statistikeInputDialogKalkulatoriFFMI); 


    statistikeInputDialogKalkulatoriFFMI.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      final Dialog dialog = new Dialog(StatistikeInputMain.this); 
      dialog.setContentView(R.layout.dialog_statistike_uredi_ffmi); 
      dialog.setTitle(getResources().getString(R.string.dialog_uredi1RM)); 
      dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; 
      dialog.show(); 
      RadioGroup radioGrupaDialogFFMI = (RadioGroup) findViewById(R.id.radioGrupaDialogFFMI); 
      final RadioButton radioButtonDialogFFMIMetric = (RadioButton) findViewById(R.id.radioButtonDialogFFMIMetric); 
      final RadioButton radioButtonDialogFFMIImperial = (RadioButton) findViewById(R.id.radioButtonDialogFFMIImperial); 
      textTezinaDialogFFMI = (TextView)dialog. findViewById(R.id.textTezinaDialogFFMI); 
      textBodyFatDialogFFMI = (TextView)dialog. findViewById(R.id.textBodyFatDialogFFMI); 
      textVisinaDialogMetricFFMI = (TextView)dialog. findViewById(R.id.textVisinaDialogMetricFFMI); 
      textVisinaDialogImperialFFMI = (TextView)dialog. findViewById(R.id.textVisinaDialogImperialFFMI); 
      editTezinaDialogFFMI = (EditText)dialog. findViewById(R.id.editTezinaDialogFFMI); 
      editBodyFatDialogFFMI = (EditText)dialog. findViewById(R.id.editBodyFatDialogFFMI); 
      editVisinaDialogMetricFFMI = (EditText)dialog. findViewById(R.id.editVisinaDialogMetricFFMI); 
      editVisinaDialogImperialFFMIFEET = (EditText)dialog. findViewById(R.id.editVisinaDialogImperialFFMIFEET); 
      editVisinaDialogImperialFFMIINCH = (EditText)dialog. findViewById(R.id.editVisinaDialogImperialFFMIINCH); 
      buttonDialogIzracunajFFMI = (Button)dialog. findViewById(R.id.buttonDialogIzracunajFFMI); 
      editTextStatistikeFFMI = (EditText)dialog. findViewById(R.id.editTextStatistikeFFMI); 

      textVisinaDialogMetricFFMI.setVisibility(View.GONE); 
      textVisinaDialogImperialFFMI.setVisibility(View.GONE); 
      editVisinaDialogMetricFFMI.setVisibility(View.GONE); 
      editVisinaDialogImperialFFMIFEET.setVisibility(View.GONE); 
      editVisinaDialogImperialFFMIINCH.setVisibility(View.GONE); 

      switch (v.getId()) { 
       case R.id.radioButtonDialogFFMIMetric: 
      boolean checked = ((RadioButton) v).isChecked(); 

        if (checked) 

         textTezinaDialogFFMI.setVisibility(View.VISIBLE); 
        textBodyFatDialogFFMI.setVisibility(View.VISIBLE); 
        textVisinaDialogMetricFFMI.setVisibility(View.VISIBLE); 
        editTezinaDialogFFMI.setVisibility(View.VISIBLE); 
        editBodyFatDialogFFMI.setVisibility(View.VISIBLE); 
        editVisinaDialogMetricFFMI.setVisibility(View.VISIBLE); 
        buttonDialogIzracunajFFMI.setVisibility(View.VISIBLE); 

        buttonDialogIzracunajFFMI.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          double tezina = Double.parseDouble(editTezinaDialogFFMI.getText().toString()); 
          double bodyfat = Double.parseDouble(editBodyFatDialogFFMI.getText().toString()); 
          double visina = Double.parseDouble(editVisinaDialogMetricFFMI.getText().toString()); 
          double rezultatLean = 0; 
          double rezultatFFMI = 0; 

          rezultatLean = tezina * (1.0 - (bodyfat/100.0)); 
          rezultatLean = Math.round(rezultatLean * 100.0)/100.0; 


          rezultatFFMI = (rezultatLean/2.2)/(((visina/2.54) * 0.0254) * ((visina/2.54) * 0.0254)) * 2.20462; 
          rezultatFFMI = Math.round(rezultatFFMI * 100.0)/100.0; 
          editTextStatistikeFFMI.setText(Double.toString(rezultatFFMI)); 

          dialog.dismiss(); 

         } 
        }); 

       case R.id.radioButtonDialogFFMIImperial: 
        boolean checked1 = ((RadioButton) v).isChecked(); 

        if (checked1) 

         textTezinaDialogFFMI.setVisibility(View.VISIBLE); 
        textBodyFatDialogFFMI.setVisibility(View.VISIBLE); 
        textVisinaDialogImperialFFMI.setVisibility(View.VISIBLE); 
        editTezinaDialogFFMI.setVisibility(View.VISIBLE); 
        editBodyFatDialogFFMI.setVisibility(View.VISIBLE); 
        editVisinaDialogImperialFFMIFEET.setVisibility(View.VISIBLE); 
        editVisinaDialogImperialFFMIINCH.setVisibility(View.VISIBLE); 
        buttonDialogIzracunajFFMI.setVisibility(View.VISIBLE); 
        buttonDialogIzracunajFFMI.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          double tezina = Double.parseDouble(editTezinaDialogFFMI.getText().toString()); 
          double bodyfat = Double.parseDouble(editBodyFatDialogFFMI.getText().toString()); 
          double visinaFEET = Double.parseDouble(editVisinaDialogImperialFFMIFEET.getText().toString()); 
          double visinaINCH = Double.parseDouble(editVisinaDialogImperialFFMIINCH.getText().toString()); 
          double rezultatLean = 0; 
          double rezultatFFMI = 0; 


          rezultatLean = tezina * (1.0 - (bodyfat/100.0)); 
          rezultatLean = Math.round(rezultatLean * 100.0)/100.0; 


          rezultatFFMI = (rezultatLean/2.2)/(((visinaFEET * 12.0 + visinaINCH) * 0.0254) * ((visinaFEET * 12.0 + visinaINCH) * 0.0254)); 
          rezultatFFMI = Math.round(rezultatFFMI * 100.0)/100.0; 
          editTextStatistikeFFMI.setText(Double.toString(rezultatFFMI)); 

          dialog.dismiss(); 


         } 

        }); 
      } 
+0

在'dialog_statistike_uredi_ffmi.xml'內的父視圖中使用'Clickable =「true」' – Kathi

+0

不能正常工作.... – DaxHR

回答

0

你所有的隱藏/顯示+視圖的OnClick回調這是不對的內部通貨膨脹的邏輯,所有這一切都應該將DialogFragment類(使用DialogFragment而不是內被寫入您正在使用的對話框,因爲它是推薦的方式)

隱藏和顯示您的UI控件以及對話框的Button和RadioButton的clickListeners的邏輯應該分開編寫 - 例如:在OnCreate你的DialogFragment。