From 8afe942031583c36c3b001c4bd9bf9eaef5a710f Mon Sep 17 00:00:00 2001 From: Alfred Date: Wed, 10 May 2017 23:03:28 +0000 Subject: [PATCH 1/2] add addTextChangedListener for EditText --- README.md | 3 +- .../questionnaireview/MainActivity.java | 39 ++++++++----------- .../QuestionnaireView.java | 33 +++++++++++----- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 680ab8f..1bac919 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,9 @@ APIs offered by **QuestionnaireView**. |APIs | Usage| |---|---| |setQuestion(String text)|Set the question on a webview| -|setViewType(int viewType)|Set the viewType to either radio, edittext or check list| +|setViewType(int viewType)|Set the viewType to either RADIO, EDITTEXT or CHECKLIST| |addRadioItemListener(OnRadioItemClickListener listener)|Sets a listener for radioButton| |addCheckItemListener(OnCheckItemClickListener listener)|Sets a listener for check list| |addOnEditorActionListener(OnEditorActionListener listener)|Sets a listener for EditText| +|addTextChangedListener(TextWatcher watcher)|Sets a watcher for EditText| |setAnswers(CharSequence[] answers)|Sets all possible answers for radio and checklist| diff --git a/app/src/main/java/com/alfredayibonte/questionnaireview/MainActivity.java b/app/src/main/java/com/alfredayibonte/questionnaireview/MainActivity.java index d2daf39..81b77e7 100644 --- a/app/src/main/java/com/alfredayibonte/questionnaireview/MainActivity.java +++ b/app/src/main/java/com/alfredayibonte/questionnaireview/MainActivity.java @@ -1,42 +1,37 @@ package com.alfredayibonte.questionnaireview; -import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; import android.util.Log; -import android.view.KeyEvent; -import android.view.inputmethod.EditorInfo; -import android.view.inputmethod.InputMethodManager; -import android.widget.TextView; - import com.alfredayibonte.questionnaireviewlib.QuestionnaireView; -import com.alfredayibonte.questionnaireviewlib.adapters.CheckListAdapter; -import com.alfredayibonte.questionnaireviewlib.adapters.RadioListAdapter; -import com.alfredayibonte.questionnaireviewlib.models.Answer; import com.alfredayibonte.questionnaireviewlib.utils.AnswerType; -import java.util.ArrayList; -import java.util.List; - -public class MainActivity extends AppCompatActivity implements - RadioListAdapter.OnRadioItemClickListener { +public class MainActivity extends AppCompatActivity implements TextWatcher { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); QuestionnaireView questionnaireView = (QuestionnaireView)findViewById(R.id.questionnaire); - questionnaireView.setQuestion("What is the name of this library ?"); - questionnaireView.setViewType(AnswerType.CHECKLIST); - CharSequence[] answers = new CharSequence[]{ - "Questionnaire", "QuestionnaireView", "Question"}; - questionnaireView.setAnswers(answers); - questionnaireView.addRadioItemListener(this); + questionnaireView.setQuestion("

What is the name of this library ?

"); + questionnaireView.setViewType(AnswerType.EDITTEXT); + questionnaireView.addTextChangedListener(this); } + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } @Override - public void onRadioItemClick(List answers) { - Log.e("radio answers: ", answers.toString()); + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + Log.e(MainActivity.class.getSimpleName(), charSequence.toString()); + } + + @Override + public void afterTextChanged(Editable editable) { + } } diff --git a/questionnaireviewlib/src/main/java/com/alfredayibonte/questionnaireviewlib/QuestionnaireView.java b/questionnaireviewlib/src/main/java/com/alfredayibonte/questionnaireviewlib/QuestionnaireView.java index 96690ad..8c2ad22 100644 --- a/questionnaireviewlib/src/main/java/com/alfredayibonte/questionnaireviewlib/QuestionnaireView.java +++ b/questionnaireviewlib/src/main/java/com/alfredayibonte/questionnaireviewlib/QuestionnaireView.java @@ -4,6 +4,7 @@ import android.content.res.TypedArray; import android.graphics.Color; import android.text.InputType; +import android.text.TextWatcher; import android.util.AttributeSet; import android.view.inputmethod.EditorInfo; import android.webkit.WebView; @@ -78,7 +79,7 @@ private void drawInnerViews(Context context, AttributeSet attrs){ addView(listView ); //creation & addition of editText - editTv = new EditText(context); + editTv = new EditText(context, attrs); editTv.setVisibility(GONE); editTv.setId(android.R.id.text1); editTv.setLayoutParams( @@ -154,12 +155,6 @@ public void setQuestion(String content){ public void setAnswers(List answers){ this.answers = answers; - } - - public void setAnswers(CharSequence[] answers){ - this.answers = new ArrayList<>(); - for(CharSequence item : answers) - this.answers.add(new Answer(String.valueOf(item))); switch (viewType){ case AnswerType.CHECKLIST: checkAdapter = new CheckListAdapter(getContext(), this.answers); @@ -170,7 +165,22 @@ public void setAnswers(CharSequence[] answers){ listView.setAdapter(radioAdapter); break; } - if(question != null) question.setAnswers(this.answers); + if(question != null && !answers.isEmpty()) + question.setAnswers(this.answers); + } + + public void setAnswers(CharSequence[] answers){ + this.answers = new ArrayList<>(); + for(CharSequence item : answers) + this.answers.add(new Answer(String.valueOf(item))); + setAnswers(this.answers); + } + + public void setAnswers(ArrayList answers){ + this.answers = new ArrayList<>(); + for(String item : answers) + this.answers.add(new Answer(String.valueOf(item))); + setAnswers(this.answers); } @@ -188,7 +198,12 @@ public void addOnEditorActionListener(EditText.OnEditorActionListener editorActi editTv.setOnEditorActionListener(editorActionListener); } - public String getResponseFromEditText(){ + public void addTextChangedListener(TextWatcher textWatcher){ + editTv.addTextChangedListener(textWatcher); + } + + + public String getTextFromEditText(){ return editTv.getText().toString(); } From 27d0144253cc6acb856da91477df4fbedb618254 Mon Sep 17 00:00:00 2001 From: Alfred Date: Fri, 12 May 2017 06:53:16 +0000 Subject: [PATCH 2/2] remove unnecessary typecast --- build.gradle | 2 +- .../alfredayibonte/questionnaireviewlib/QuestionnaireView.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 1ea4bd0..b78a0b8 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.0' + classpath 'com.android.tools.build:gradle:2.3.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/questionnaireviewlib/src/main/java/com/alfredayibonte/questionnaireviewlib/QuestionnaireView.java b/questionnaireviewlib/src/main/java/com/alfredayibonte/questionnaireviewlib/QuestionnaireView.java index 8c2ad22..2b54b5a 100644 --- a/questionnaireviewlib/src/main/java/com/alfredayibonte/questionnaireviewlib/QuestionnaireView.java +++ b/questionnaireviewlib/src/main/java/com/alfredayibonte/questionnaireviewlib/QuestionnaireView.java @@ -179,7 +179,7 @@ public void setAnswers(CharSequence[] answers){ public void setAnswers(ArrayList answers){ this.answers = new ArrayList<>(); for(String item : answers) - this.answers.add(new Answer(String.valueOf(item))); + this.answers.add(new Answer(item)); setAnswers(this.answers); }