我的佈局是:驗證的EditText是空的,並等待用戶輸入數據
1 EditText (id = etvalue1)
1 EditText (id = etvalue2)
1 Button (id = save)
我需要知道如何做這些小部件如下:
- 如果在用戶點擊按鈕EditText爲空將顯示一條消息並等待用戶輸入值。
- 顯示消息,直到用戶輸入值並輸入值(
etvalue1
+etvalue2
)並顯示結果。
我的佈局是:驗證的EditText是空的,並等待用戶輸入數據
1 EditText (id = etvalue1)
1 EditText (id = etvalue2)
1 Button (id = save)
我需要知道如何做這些小部件如下:
etvalue1
+ etvalue2
)並顯示結果。在onclicklistener該按鈕執行以下操作
if(edit1.getText().equals("") || edit2.getText().equals(""))
{
//toast the error message
return;
}
//calculate the result
的最長時間爲土司是Toast.LENGTH_LONG。如果您希望顯示消息直到用戶在edittext中輸入數據,然後使用textview顯示消息。在兩個EditText上設置onTextChangedListener,以檢查文本是否更改。在afterTextChanged方法中,您可以進行計算。
如果您想在用戶在edittext
進入值進行檢測,你需要使用事件觀察其變化在Counting Chars in EditText Changed Listener
閱讀在reference
或者相關的方法,您可以對按鈕的單擊事件進行檢查:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(etvalue1.getText().toString().equals(''))
//empty text condition
}
});
Y OU可以使用以下代碼來確定的EditText爲空:
String sEdit1 = Edit1.getText().toString();
if (sEdit1.matches(""))
{
Toast.makeText(getBaseContext(), "EditText is empty", Toast.LENGTH_SHORT).show();
return;
}
if(edit1.getText().equals("") || edit1.getText().equals(null)) {
//toast the message
} else {
//calculate the result
}
在活動類編寫代碼在onCreate方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText1 = (EditText)findViewById(R.id.etvalue1);
editText2 = (EditText)findViewById(R.id.etvalue2);
button = (Button)findViewById(R.id.save);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!(editText1.getText().toString().equalsIgnoreCase(""))){
if(!(editText2.getText().toString()).equalsIgnoreCase("")){
addNumbers();
} else {
Toast.makeText(getApplicationContext(),
"Enter no.", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(),
"Enter no.", Toast.LENGTH_LONG).show();
}
}
private void addNumbers() {
}
});
}
}
在的addNumbers方法得到來自EditTest的值,並做任何你想做的事情。
if(edit1.getText().equals("") || edit2.getText().equals(""))
{
//toast the error message
Dialog dialog=new Dialog(getApplicationContext());
LinearLayout llView=new LinearLayout(getApplicationContext());
EditText editText1=new EditText(getApplicationContext());
EditText editText2=new EditText(getApplicationContext());
wsutText2.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
//do your calculations
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
editText2.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
//do your calculations
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
llView.addView(editText1);
llView.addView(editText2);
dialog.setContentView(llView);
dialog.show();
return;
}
如果您對此答案滿意,請接受它 – 2012-02-07 11:01:17