我有这个应用程序,它在列表视图中输出我在状态文本字段中输入的内容。但是,我需要添加一个函数,如果状态文本字段为空,则POST按钮不应该被禁用。我实际上读到了包含文本观察者的内容,但我不确定如何整合它。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.FitWindowsFrameLayout;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ArrayList<FriendStatusMessage> mMessageList = new ArrayList<FriendStatusMessage>();
ArrayAdapter<FriendStatusMessage> mAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnPost = (Button) findViewById(R.id.btn_post);
btnPost.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
postMessage();
return;
}
}
);
mAdapter = new ArrayAdapter<FriendStatusMessage>(this, android.R.layout.simple_list_item_1, mMessageList);
ListView lstMessages = (ListView) findViewById(R.id.lst_messages);
lstMessages.setAdapter(mAdapter);
}
private void postMessage() {
EditText edtUsername = (EditText) findViewById(R.id.edt_username);
EditText edtStatusMessage = (EditText) findViewById(R.id.edt_status_msg);
String userStr = edtUsername.getText().toString();
String messageStr = edtStatusMessage.getText().toString();
mMessageList.add(new FriendStatusMessage(userStr, messageStr));
mAdapter.notifyDataSetChanged();
return;
}
private class FriendStatusMessage {
private String mUsername = "";
private String mMessage = "";
public FriendStatusMessage(String username, String statusMessage) {
mUsername = username;
mMessage = statusMessage;
}
public String getmUsername()
{
return mUsername;
}
public String getmMessage ()
{
return mMessage;
}
public String toString() {
return getmUsername() + ": " + getmMessage() + "\n";
}
}
}
答案 0 :(得分:0)
如果不采取行动呢?
private void postMessage() {
EditText edtUsername = (EditText) findViewById(R.id.edt_username);
EditText edtStatusMessage = (EditText) findViewById(R.id.edt_status_msg);
String userStr = edtUsername.getText().toString();
String messageStr = edtStatusMessage.getText().toString();
boolean doAction = false;
if (userStr.isEmpty()) {
edtUsername.setError("Must enter username");
}
if (messageStr.isEmpty()) {
edtStatusMessage.setError("Must enter status");
}
doAction = true;
if (doAction) {
mMessageList.add(new FriendStatusMessage(userStr, messageStr));
mAdapter.notifyDataSetChanged();
}
}
否则,您需要将这些内容移至onCreate
EditText edtUsername = (EditText) findViewById(R.id.edt_username);
EditText edtStatusMessage = (EditText) findViewById(R.id.edt_status_msg);
正确addTextChangedListener
到那些。
答案 1 :(得分:0)
试试这个,这是一个更好的选择
void postMessage() {
EditText edtUsername = (EditText) findViewById(R.id.edt_username);
edtUsername.setError(null);
EditText edtStatusMessage = (EditText) findViewById(R.id.edt_status_msg);
edtStatusMessage.setError(null);
String userStr = edtUsername.getText().toString();
String messageStr = edtStatusMessage.getText().toString();
if (TextUtils.isEmpty(userStr)) {
edtUsername.setError("This shouldn't be blank");
edtUsername.requestFocus();
}else (TextUtils.isEmpty(messageStr)) {
edtStatusMessage.setError("This shouldn't be blank");
edtStatusMessage.requestFocus();
}else{
mMessageList.add(new FriendStatusMessage(userStr, messageStr));
mAdapter.notifyDataSetChanged();
}
}
答案 2 :(得分:0)
您需要输入文字验证和&amp; addTextChangedListener用于发布按钮操作。空文本验证后,您可以启用发布按钮来执行后期操作。您还可以在edtStatusMessage上使用TextWatcher来执行后期操作。
private EditText edtUsername, edtStatusMessage ;
private Button btnPost;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtUsername = (EditText) findViewById(R.id.edt_username);
edtStatusMessage = (EditText) findViewById(R.id.edt_status_msg);
btnPost = (Button) findViewById(R.id.btn_post);
btnPost.setEnabled(false);
edtStatusMessage.addTextChangedListener(listenerTextChangedStatus );
}
private TextWatcher listenerTextChangedStatus = new TextWatcher() {
public void afterTextChanged(Editable editable) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
if(!edtUsername.getText().toString().isEmpty() && !s.getText().toString().isEmpty()){
btnPost.setEnabled(true);
// do action
btnPost.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
postMessage();
return;
}
}
);
}else{
btnPost.setEnabled(false);
edtStatusMessage.setFocus(true);
}
}
};
for more information follow the link
http://stackoverflow.com/questions/8543449/how-to-use-the-textwatcher-class-in-android