2016-05-12 95 views
1

我試圖從EditTexts檢索用戶輸入的值,但沒有顯示出來。這裏是我的代碼Android EditText值不顯示

public class RechargeCarte extends Fragment { 
private EditText cardnummer,transfersolde,bannummer; 
RequestQueue requestQueue; 
String s1,s2,s3; 

public RechargeCarte() { 
    // Required empty public constructor 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_recharge_carte, container, false); 
    final Button load = (Button) rootView.findViewById(R.id.btn_load_card); 
    cardnummer = (EditText) rootView.findViewById(R.id.et_card_number); 
    transfersolde = (EditText) rootView.findViewById(R.id.et_transfer_solde); 
    bannummer = (EditText) rootView.findViewById(R.id.et_ban_number); 
    requestQueue = Volley.newRequestQueue(getActivity()); 
    s1 = cardnummer.getText().toString(); 
    s2 = bannummer.getText().toString(); 
    s3 = transfersolde.getText().toString(); 

    load.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show(); 
      Log.e("CARDNUMBER", "aaaa"+s1); 
      Log.e("BANNUMBER","aaaa"+s2); 
      Log.e("TRANSFERT","aaaa"+s3); 
     } 
    }); 
    return rootView; 
} 
} 

敬酒不出現,但日誌結果總是「AAAA」所輸入的值顯示不出來。

如果任何人可以幫助..謝謝:))

+0

從裏面的EditText''的onClick方法獲得的輸入值。如果您在'onCreateView'內調用'getText'方法,那麼它將返回空字符串而不是用戶輸入的字符串。 –

+0

它並沒有真正有所作爲,但是您希望將「Log.d」用於這些類型的日誌記錄。 –

回答

0

嘗試這樣的..
按照活動的生命週期,onCreate()方法只運行在第一次創建Actviity時,你可以選擇把在load.setOnClickListener

load.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
    s1 = cardnummer.getText().toString(); 
    s2 = bannummer.getText().toString(); 
    s3 = transfersolde.getText().toString(); 
      Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show(); 
      Log.e("CARDNUMBER", "aaaa"+s1); 
      Log.e("BANNUMBER","aaaa"+s2); 
      Log.e("TRANSFERT","aaaa"+s3); 
     } 
    }); 
1

你的代碼得到「」值bcoz其來看一下之前....

使用此它解決您的問題。 ......

load.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show(); 
      s1 = cardnummer.getText().toString(); 
      s2 = bannummer.getText().toString(); 
      s3 = transfersolde.getText().toString(); 
      Log.e("CARDNUMBER", "aaaa"+s1); 
      Log.e("BANNUMBER","aaaa"+s2); 
      Log.e("TRANSFERT","aaaa"+s3); 
     } 
    }); 

享受編碼........

0

你必須得到EditTex牛逼部件的文本,當您點擊Load按鈕,即:

load.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show(); 
     s1 = cardnummer.getText().toString(); 
     s2 = bannummer.getText().toString(); 
     s3 = transfersolde.getText().toString(); 
     Log.e("CARDNUMBER", "aaaa"+s1); 
     Log.e("BANNUMBER","aaaa"+s2); 
     Log.e("TRANSFERT","aaaa"+s3); 
    } 
}); 
0

代碼可以從EditText獲取值只有當你在Button點擊調用方法getText()

就像你需要替換如下代碼:

load.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      s1 = cardnummer.getText().toString(); 
      s2 = bannummer.getText().toString(); 
      s3 = transfersolde.getText().toString(); 
      Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show(); 
      Log.e("CARDNUMBER", "aaaa"+s1); 
      Log.e("BANNUMBER","aaaa"+s2); 
      Log.e("TRANSFERT","aaaa"+s3); 
     } 
    });