Android EditText值不会显示

时间:2016-05-12 11:12:19

标签: android android-edittext

我试图从EditTexts中检索用户输入的值,但没有显示任何内容。这是我的代码

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;
}
}

Toast确实显示,但Log结果总是" aaaa"输入的值不会显示。

如果有人可以提供帮助..谢谢:))

4 个答案:

答案 0 :(得分: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);
        }
    });

享受编码........

答案 1 :(得分:0)

单击加载按钮时,您必须获取EditText小部件文本,即:

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);
    }
});

答案 2 :(得分: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);
        }
    });

答案 3 :(得分:0)

只有在EditText点击时调用方法getText(),才能从Button获取值。

就像你需要更换代码一样:

 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);
        }
    });