Android Open Source - OpenHSK Create Word Activity






From Project

Back to project page OpenHSK.

License

The source code is released under:

This work is licensed under a Creative Commons Attribution 3.0 Unported License. Original author of word lists: http://lingomi.com/ Original author of definitions: http://cc-cedict.org Original autho...

If you think the Android project OpenHSK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package edu.openhsk;
//from w w w. ja v  a2 s. c o m
import edu.openhsk.models.Hanzi;
import edu.openhsk.repository.DatabaseHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CreateWordActivity extends Activity {
  protected static final String EDITED_WORD_ID = "edu.openhsk.createword.id";
  protected static final String NEW_WORD_HANZI = "edu.openhsk.createword.hanzi";
  protected static final String NEW_WORD_PINYIN = "edu.openhsk.createword.pinyin";
  protected static final String NEW_WORD_DEFINITION = "edu.openhsk.createword.definition";
  protected static final String ID_OF_WORD_TO_EDIT_EXTRA = "edu.openhsk.createword.editWordId";
  private EditText hanziInput;
  private EditText pinyinInput;
  private EditText defInput;
  private int wordId;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_word);
    setTitle(getString(R.string.new_word));
    
    DatabaseHelper dbh = new DatabaseHelper(this);
    
    hanziInput = (EditText) findViewById(R.id.hanziInput);
    pinyinInput = (EditText) findViewById(R.id.pinyinInput);
    defInput = (EditText) findViewById(R.id.definitionInput);
    //The xml flags for InputType are allegedly very manufacturer-dependent. This might be the safest method...
    hanziInput.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    pinyinInput.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    defInput.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    
    Intent intent = getIntent();
    wordId = intent.getIntExtra(ID_OF_WORD_TO_EDIT_EXTRA, -1);
    if (wordId != -1) {
      Hanzi word = dbh.getWordById(wordId);
      hanziInput.setText(word.getWord());
      pinyinInput.setText(word.getPinyin());
      defInput.setText(word.getDefinition());
    }
    
    Button saveButton = (Button) findViewById(R.id.create_word_save_button);
    saveButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        String hanzi = hanziInput.getText().toString();
        String pinyin = pinyinInput.getText().toString();
        String def = defInput.getText().toString();
        if (allInputsValid(new String[]{hanzi, pinyin, def})) {
          Intent intent = new Intent();
          intent.putExtra(EDITED_WORD_ID, wordId);
          intent.putExtra(NEW_WORD_HANZI, hanzi);
          intent.putExtra(NEW_WORD_PINYIN, pinyin);
          intent.putExtra(NEW_WORD_DEFINITION, def);
          setResult(RESULT_OK, intent);
          finish();
        } else {
          Toast.makeText(CreateWordActivity.this, R.string.invalid_fields, 
              Toast.LENGTH_SHORT).show();
        }
      }
    });

    Button cancelButton = (Button) findViewById(R.id.create_word_cancel_button);
    cancelButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        setResult(RESULT_CANCELED);
        finish();
      }
    });
  }

  private boolean allInputsValid(String... inputs) {
    for (String str : inputs) {
      if (str == null || str.length() == 0) {
        return false;
      }
    }
    return true;
  }
}




Java Source Code List

edu.openhsk.CharacterListActivity.java
edu.openhsk.CharacterViewActivity.java
edu.openhsk.CreateWordActivity.java
edu.openhsk.CreateWordListActivity.java
edu.openhsk.ExamActivity.java
edu.openhsk.ExamResultActivity.java
edu.openhsk.MainActivity.java
edu.openhsk.QuizActivity.java
edu.openhsk.WordListSelectionActivity.java
edu.openhsk.adapters.WordListViewBinder.java
edu.openhsk.adapters.WordViewBinder.java
edu.openhsk.models.ExamResult.java
edu.openhsk.models.Hanzi.java
edu.openhsk.models.QuizHanzi.java
edu.openhsk.models.WordList.java
edu.openhsk.repository.DatabaseHelper.java
edu.openhsk.repository.DatabaseMetadata.java
edu.openhsk.repository.HanziRepository.java
edu.openhsk.repository.HelpfulCursor.java
edu.openhsk.repository.QuizRepository.java
edu.openhsk.repository.SimpleCursorLoader.java
edu.openhsk.repository.WordListRepository.java
edu.openhsk.service.QuizService.java
edu.openhsk.utils.AsyncSoundPlayer.java
edu.openhsk.utils.CSVExporter.java
edu.openhsk.utils.CSVParser.java
edu.openhsk.utils.PinyinReplacer.java
edu.openhsk.utils.SoundManager.java
edu.openhsk.views.BarGraph.java
edu.openhsk.views.Bar.java
edu.openhsk.views.ExamResultPieChart.java