2017-05-01 62 views
-1

我對編碼非常陌生,所以可能會遇到各種各樣的問題,但經過一段時間的搜索之後,我找不到它有什麼問題,我沒有得到任何錯誤或崩潰,按鈕只是沒做什麼?EditText在ImageButton新聞上沒有改變?

目的是讓橙色箭頭增加和減1。在中間的數字

應用截圖:

App Screenshot

我不知道是什麼碼向你展示,所以這裏是最重要的:

T3_TrampetFragment.java

import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class T3_TrampetFragment extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.t3_trampetlayout, container, false); 
     } 
} 

t3_trampetlayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".T3_TrampetFragment"> 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TableRow 
      android:id="@+id/tblrow_title" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <TextView 
       android:id="@+id/txt_title" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" 
       android:text="Tariff Calculator" 
       android:textAlignment="center" 
       android:textColor="@color/colorPrimaryDark" 
       android:textSize="36sp" 
       android:textStyle="bold" /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tblrow_rotations" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:paddingBottom="10dp" 
      android:paddingTop="10dp"> 

      <TextView 
       android:id="@+id/txt_rotations" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" 
       android:text="Rotations" 
       android:textAlignment="center" 
       android:textColor="@color/colorPrimary" 
       android:textSize="24sp" /> 

      <ImageButton 
       android:id="@+id/btn_r_minus" 
       android:layout_height="40dp" 
       android:layout_marginTop="5dp" 
       android:backgroundTint="?android:attr/colorBackground" 
       android:clickable="true" 
       android:foreground="@android:drawable/ic_media_play" 
       android:foregroundTint="@color/colorSecondary" 
       android:hapticFeedbackEnabled="true" 
       android:rotation="180" /> 

      <Space 
       android:layout_width="10dp" 
       android:layout_height="wrap_content" /> 

      <EditText 
       android:id="@+id/Rnum" 
       android:layout_width="50dp" 
       android:layout_height="wrap_content" 
       android:background="?android:attr/colorBackground" 
       android:focusable="false" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" 
       android:text="@string/sRnum" 
       android:textAlignment="center" 
       android:textColor="?attr/colorControlNormal" 
       android:textSize="24sp" 
       android:textStyle="bold" /> 

      <Space 
       android:layout_width="10dp" 
       android:layout_height="wrap_content" /> 

      <ImageButton 
       android:id="@+id/btn_r_plus" 
       android:layout_height="40dp" 
       android:layout_marginTop="5dp" 
       android:backgroundTint="?android:attr/colorBackground" 
       android:clickable="true" 
       android:foreground="@android:drawable/ic_media_play" 
       android:foregroundTint="@color/colorSecondary" 
       android:hapticFeedbackEnabled="true" /> 

      <Space 
       android:layout_width="20dp" 
       android:layout_height="wrap_content" /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tblrow_pike" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:paddingBottom="10dp" 
      android:paddingTop="10dp"> 

      <TextView 
       android:id="@+id/txt_pike" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" 
       android:text="Pike Bonus" 
       android:textAlignment="center" 
       android:textColor="@color/colorPrimary" 
       android:textSize="24sp" /> 

      <ImageButton 
       android:id="@+id/btn_p_minus" 
       android:layout_height="40dp" 
       android:layout_marginTop="5dp" 
       android:backgroundTint="?android:attr/colorBackground" 
       android:clickable="true" 
       android:foreground="@android:drawable/ic_media_play" 
       android:foregroundTint="@color/colorSecondary" 
       android:hapticFeedbackEnabled="true" 
       android:rotation="180" /> 

      <Space 
       android:layout_width="10dp" 
       android:layout_height="wrap_content" /> 

      <EditText 
       android:id="@+id/Pnum" 
       android:layout_width="50dp" 
       android:layout_height="wrap_content" 
       android:background="?android:attr/colorBackground" 
       android:focusable="false" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" 
       android:text="@string/sPnum" 
       android:textAlignment="center" 
       android:textColor="?attr/colorControlNormal" 
       android:textSize="24sp" 
       android:textStyle="bold" /> 

      <Space 
       android:layout_width="10dp" 
       android:layout_height="wrap_content" /> 

      <ImageButton 
       android:id="@+id/btn_p_plus" 
       android:layout_height="40dp" 
       android:layout_marginTop="5dp" 
       android:backgroundTint="?android:attr/colorBackground" 
       android:clickable="true" 
       android:foreground="@android:drawable/ic_media_play" 
       android:foregroundTint="@color/colorSecondary" 
       android:hapticFeedbackEnabled="true" /> 

      <Space 
       android:layout_width="20dp" 
       android:layout_height="wrap_content" /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tblrow_straight" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:paddingBottom="10dp" 
      android:paddingTop="10dp"> 

      <TextView 
       android:id="@+id/txt_straight" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" 
       android:text="Straight Bonus" 
       android:textAlignment="center" 
       android:textColor="@color/colorPrimary" 
       android:textSize="24sp" /> 

      <ImageButton 
       android:id="@+id/btn_s_minus" 
       android:layout_height="40dp" 
       android:layout_marginTop="5dp" 
       android:backgroundTint="?android:attr/colorBackground" 
       android:clickable="true" 
       android:foreground="@android:drawable/ic_media_play" 
       android:foregroundTint="@color/colorSecondary" 
       android:hapticFeedbackEnabled="true" 
       android:rotation="180" /> 

      <Space 
       android:layout_width="10dp" 
       android:layout_height="wrap_content" /> 

      <EditText 
       android:id="@+id/Snum" 
       android:layout_width="50dp" 
       android:layout_height="wrap_content" 
       android:background="?android:attr/colorBackground" 
       android:focusable="false" 
       android:inputType="number" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" 
       android:text="@string/sSnum" 
       android:textAlignment="center" 
       android:textColor="?attr/colorControlNormal" 
       android:textSize="24sp" 
       android:textStyle="bold" /> 

      <Space 
       android:layout_width="10dp" 
       android:layout_height="wrap_content" /> 

      <ImageButton 
       android:id="@+id/btn_s_plus" 
       android:layout_height="40dp" 
       android:layout_marginTop="5dp" 
       android:backgroundTint="?android:attr/colorBackground" 
       android:clickable="true" 
       android:foreground="@android:drawable/ic_media_play" 
       android:foregroundTint="@color/colorSecondary" 
       android:hapticFeedbackEnabled="true" /> 

      <Space 
       android:layout_width="20dp" 
       android:layout_height="wrap_content" /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tblrow_twist" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:paddingBottom="10dp" 
      android:paddingTop="10dp"> 

      <TextView 
       android:id="@+id/txt_twist" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" 
       android:text="Twists" 
       android:textAlignment="center" 
       android:textColor="@color/colorPrimary" 
       android:textSize="24sp" /> 

      <ImageButton 
       android:id="@+id/btn_t_minus" 
       android:layout_height="40dp" 
       android:layout_marginTop="5dp" 
       android:backgroundTint="?android:attr/colorBackground" 
       android:clickable="true" 
       android:foreground="@android:drawable/ic_media_play" 
       android:foregroundTint="@color/colorSecondary" 
       android:hapticFeedbackEnabled="true" 
       android:rotation="180" /> 

      <Space 
       android:layout_width="10dp" 
       android:layout_height="wrap_content" /> 

      <EditText 
       android:id="@+id/Tnum" 
       android:layout_width="50dp" 
       android:layout_height="wrap_content" 
       android:background="?android:attr/colorBackground" 
       android:focusable="false" 
       android:inputType="numberDecimal" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" 
       android:text="@string/sTnum" 
       android:textAlignment="center" 
       android:textColor="?attr/colorControlNormal" 
       android:textSize="24sp" 
       android:textStyle="bold" /> 

      <Space 
       android:layout_width="10dp" 
       android:layout_height="wrap_content" /> 

      <ImageButton 
       android:id="@+id/btn_t_plus" 
       android:layout_height="40dp" 
       android:layout_marginTop="5dp" 
       android:backgroundTint="?android:attr/colorBackground" 
       android:clickable="true" 
       android:foreground="@android:drawable/ic_media_play" 
       android:foregroundTint="@color/colorSecondary" 
       android:hapticFeedbackEnabled="true" /> 

      <Space 
       android:layout_width="20dp" 
       android:layout_height="wrap_content" /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tblrow_result" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:paddingBottom="20dp" 
      android:paddingTop="20dp"> 

      <TextView 
       android:id="@+id/txt_result" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" 
       android:text="Difficulty" 
       android:textAlignment="center" 
       android:textColor="@color/colorPrimary" 
       android:textSize="24sp" 
       android:textStyle="bold" /> 

      <android.support.design.widget.FloatingActionButton 
       android:id="@+id/_space" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:clickable="false" 
       android:rotation="180" 
       android:visibility="invisible" 
       app:backgroundTint="@color/colorSecondary" 
       app:fabSize="mini" 
       app:srcCompat="@android:drawable/ic_media_play" /> 

      <Space 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" /> 

      <EditText 
       android:id="@+id/Resultnum" 
       android:layout_width="70dp" 
       android:layout_height="wrap_content" 
       android:background="?android:attr/colorBackground" 
       android:focusable="false" 
       android:inputType="numberDecimal" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" 
       android:text="@string/sResultnum" 
       android:textAlignment="center" 
       android:textColor="?attr/colorControlNormal" 
       android:textSize="24sp" 
       android:textStyle="bold" /> 

      <Space 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" /> 

      <android.support.design.widget.FloatingActionButton 
       android:id="@+id/_space2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:clickable="false" 
       android:visibility="invisible" 
       app:backgroundTint="@color/colorSecondary" 
       app:fabSize="mini" 
       app:srcCompat="@android:drawable/ic_media_play" /> 

      <Space 
       android:layout_width="20dp" 
       android:layout_height="wrap_content" /> 

     </TableRow> 
    </TableLayout> 

</RelativeLayout> 

T3_TrampetActivity.java

import android.os.Bundle; 
import android.app.Activity; 
import android.view.View; 
import android.widget.EditText; 

public class T3_TrampetActivity extends Activity implements View.OnClickListener { 

    ImageButton BtnRMinus, BtnRPlus, BtnPMinus, BtnPPlus, BtnSMinus, BtnSPlus, BtnTMinus, BtnTPlus; 
    EditText R_num, P_num, S_num, T_num, Result_num; 
    int R_counter = (1); 
    int P_counter, S_counter = (0); 
    double T_counter = (0.0); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.t3_trampetlayout); 

     R_num = (EditText) findViewById(R.id.Rnum); 
     P_num = (EditText) findViewById(R.id.Pnum); 
     S_num = (EditText) findViewById(R.id.Snum); 
     T_num = (EditText) findViewById(R.id.Tnum); 
     Result_num = (EditText) findViewById(R.id.Resultnum); 

     BtnRMinus = (ImageButton) findViewById(R.id.btn_r_minus); 
     BtnRPlus = (ImageButton) findViewById(R.id.btn_r_plus); 
     BtnPMinus = (ImageButton) findViewById(R.id.btn_p_minus); 
     BtnPPlus = (ImageButton) findViewById(R.id.btn_p_plus); 
     BtnSMinus = (ImageButton) findViewById(R.id.btn_s_minus); 
     BtnSPlus = (ImageButton) findViewById(R.id.btn_s_plus); 
     BtnTMinus = (ImageButton) findViewById(R.id.btn_t_minus); 
     BtnTPlus = (ImageButton) findViewById(R.id.btn_t_plus); 

     BtnRMinus.setOnClickListener(this); 
     BtnRPlus.setOnClickListener(this); 
     BtnPMinus.setOnClickListener(this); 
     BtnPPlus.setOnClickListener(this); 
     BtnSMinus.setOnClickListener(this); 
     BtnSPlus.setOnClickListener(this); 
     BtnTMinus.setOnClickListener(this); 
     BtnTPlus.setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.btn_r_minus: 

       if (R_counter > 1) { 
        --R_counter; 
        R_num.setText(String.valueOf(R_counter)); 
       } 

       break; 
      case R.id.btn_r_plus: 

       if (R_counter < 3) { 
        ++R_counter; 
        R_num.setText(String.valueOf(R_counter)); 
       } 

       break; 
      case R.id.btn_p_minus: 

       if (P_counter > 0) { 
        --P_counter; 
        P_num.setText(String.valueOf(P_counter)); 
       } 

       break; 
      case R.id.btn_p_plus: 

       if (P_counter < 3) { 
        ++P_counter; 
        P_num.setText(String.valueOf(P_counter)); 
       } 

       break; 
      case R.id.btn_s_minus: 

       if (S_counter > 0) { 
        --S_counter; 
        S_num.setText(String.valueOf(S_counter)); 
       } 

       break; 
      case R.id.btn_s_plus: 

       if (S_counter < 3) { 
        ++S_counter; 
        S_num.setText(String.valueOf(S_counter)); 
       } 

       break; 
      case R.id.btn_t_minus: 

       if (T_counter > 0) { 
        T_counter = (T_counter - 0.5); 
        T_num.setText(String.valueOf(T_counter)); 
       } 

       break; 
      case R.id.btn_t_plus: 

       if (T_counter < 5.5) { 
        T_counter = (T_counter + 0.5); 
        T_num.setText(String.valueOf(T_counter)); 
       } 

       break; 
      default: 
       break; 
     } 

    } 
} 

的strings.xml

<resources> 
    <string name="app_name">TG Mobile</string> 
    <string name="in_app_name">TeamGym Mobile</string> 
    <string name="sRnum">1</string> 
    <string name="sPnum">0</string> 
    <string name="sSnum">0</string> 
    <string name="sTnum">0.0</string> 
    <string name="sResultnum">0.00</string> 
</resources> 
+0

我只是複製和粘貼你的代碼它的工作,因爲你想,但在這裏,我只使用你的活動和佈局(與刪除線工具:上下文=「。T3_TrampetFragment」)。在我的代碼中沒有添加片段(B'coz我沒有找到任何理由在代碼中使用片段)。因此,請檢查從代碼中刪除片段。 – Janak

+0

在目前的形式下,有人回答你的問題將會非常困難。我們不知道你以前是否曾經工作過,但是也許你改變了一件事,並且按鈕停止工作。這是重要的信息。 我認爲你最好的選擇是爲Android找到一些示例代碼,創建一個非常簡單的應用程序,例如一個按鈕更新一個計數器。運行該示例,然後進行小的更改。 (例如兩個計數器,兩個按鈕)如果該更改不起作用,請向* that *問題尋求幫助。代碼將小得多,人們將更有可能回答。 – leoger

+0

@Janak片段是爲了讓標籤在頂部工作,或者至少它認爲這是唯一的方法? – Connor

回答

0

創建T3_TrampetActivity這樣而從活動請致電您的片段:

public class T3_TrampetActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Fragment fragment = new T3_TrampetFragment(); 
     getSupportFragmentManager().beginTransaction() 
       .replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit(); 

    } 
} 

創建activity_main.xml中爲這樣的:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     
      android:id="@+id/fragment_frame"    
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

更新T3_TrampetFragment.java

import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class T3_TrampetFragment extends Fragment implements View.OnClickListener { 


ImageButton BtnRMinus, BtnRPlus, BtnPMinus, BtnPPlus, BtnSMinus, BtnSPlus, BtnTMinus, BtnTPlus; 
EditText R_num, P_num, S_num, T_num, Result_num; 
int R_counter = (1); 
int P_counter, S_counter = (0); 
double T_counter = (0.0); 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.t3_trampetlayout, container, false); 



    R_num = (EditText) view.findViewById(R.id.Rnum); 
    P_num = (EditText) view.findViewById(R.id.Pnum); 
    S_num = (EditText) view.findViewById(R.id.Snum); 
    T_num = (EditText) view.findViewById(R.id.Tnum); 
    Result_num = (EditText) view.findViewById(R.id.Resultnum); 

    BtnRMinus = (ImageButton) view.findViewById(R.id.btn_r_minus); 
    BtnRPlus = (ImageButton)view.findViewById(R.id.btn_r_plus); 
    BtnPMinus = (ImageButton) view.findViewById(R.id.btn_p_minus); 
    BtnPPlus = (ImageButton) view.findViewById(R.id.btn_p_plus); 
    BtnSMinus = (ImageButton) view.findViewById(R.id.btn_s_minus); 
    BtnSPlus = (ImageButton) view.findViewById(R.id.btn_s_plus); 
    BtnTMinus = (ImageButton) view.findViewById(R.id.btn_t_minus); 
    BtnTPlus = (ImageButton) view.findViewById(R.id.btn_t_plus); 

    BtnRMinus.setOnClickListener(this); 
    BtnRPlus.setOnClickListener(this); 
    BtnPMinus.setOnClickListener(this); 
    BtnPPlus.setOnClickListener(this); 
    BtnSMinus.setOnClickListener(this); 
    BtnSPlus.setOnClickListener(this); 
    BtnTMinus.setOnClickListener(this); 
    BtnTPlus.setOnClickListener(this); 

    return view; 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.btn_r_minus: 

      if (R_counter > 1) { 
       --R_counter; 
       R_num.setText(String.valueOf(R_counter)); 
      } 

      break; 
     case R.id.btn_r_plus: 

      if (R_counter < 3) { 
       ++R_counter; 
       R_num.setText(String.valueOf(R_counter)); 
      } 

      break; 
     case R.id.btn_p_minus: 

      if (P_counter > 0) { 
       --P_counter; 
       P_num.setText(String.valueOf(P_counter)); 
      } 

      break; 
     case R.id.btn_p_plus: 

      if (P_counter < 3) { 
       ++P_counter; 
       P_num.setText(String.valueOf(P_counter)); 
      } 

      break; 
     case R.id.btn_s_minus: 

      if (S_counter > 0) { 
       --S_counter; 
       S_num.setText(String.valueOf(S_counter)); 
      } 

      break; 
     case R.id.btn_s_plus: 

      if (S_counter < 3) { 
       ++S_counter; 
       S_num.setText(String.valueOf(S_counter)); 
      } 

      break; 
     case R.id.btn_t_minus: 

      if (T_counter > 0) { 
       T_counter = (T_counter - 0.5); 
       T_num.setText(String.valueOf(T_counter)); 
      } 

      break; 
     case R.id.btn_t_plus: 

      if (T_counter < 5.5) { 
       T_counter = (T_counter + 0.5); 
       T_num.setText(String.valueOf(T_counter)); 
      } 

      break; 
     default: 
      break; 
     } 
    } 
} 

t3_trampetlayout.xml對Fr使用相同的layou agment。