2017-08-19 7 views
0

私はメモを保存するためのAndroidアプリを作っています。私はeditTextをボタンクリックの前にtextViewのように振る舞わせました。 editボタンをクリックすると、editTextの値が取得されます。問題が発生すると、その値を保存して表示するときに発生します。 EditTextは、私が望むように複数の行ではなく、1行で表示されます。EditTextを複数行にできません

editTextのinputTypeプロパティをxmlファイルの複数行として宣言しましたが、editTextを初期化する際にコード内でinputTypeプロパティをnullとし、複数行にするようにnullを設定しました。それは起こりません。

XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 


<android.support.v7.widget.Toolbar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:theme="@style/AppTheme.AppBarOverlay" 
    android:background="@color/colorToolBar" 
    android:id="@+id/idToolbarNoteDetails" 
    android:padding="10dp"/> 


<android.support.design.widget.FloatingActionButton 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:id="@+id/idFloatingActionButtonEdit" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    app:srcCompat="@android:drawable/ic_menu_edit" 
    android:layout_margin="20dp"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Colors: " 
    android:textStyle="italic" 
    android:visibility="invisible" 
    android:id="@+id/idTextViewColors" 
    android:layout_below="@+id/idToolbarNoteDetails" 
    android:layout_margin="10dp" 
    android:textSize="15dp"/> 

<TextView 
    android:layout_width="25dp" 
    android:layout_height="25dp" 
    android:id="@+id/idColorRed" 
    android:visibility="invisible" 
    android:layout_margin="10dp" 
    android:layout_below="@+id/idToolbarNoteDetails" 
    android:layout_toRightOf="@+id/idTextViewColors" 
    android:background="@drawable/textview_circle_red" /> 

<TextView 
    android:layout_width="25dp" 
    android:layout_height="25dp" 
    android:visibility="invisible" 
    android:id="@+id/idColorBlue" 
    android:layout_margin="10dp" 
    android:layout_below="@id/idToolbarNoteDetails" 
    android:layout_toRightOf="@+id/idColorRed" 
    android:background="@drawable/textview_circle_blue"/> 

<TextView 
    android:layout_width="25dp" 
    android:layout_height="25dp" 
    android:id="@+id/idColorGreen" 
    android:visibility="invisible" 
    android:layout_margin="10dp" 
    android:layout_below="@id/idToolbarNoteDetails" 
    android:layout_toRightOf="@+id/idColorBlue" 
    android:background="@drawable/textview_circle_green"/> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/idTextViewColors" 
    android:layout_margin="10dp" 
    android:inputType="textShortMessage" 
    android:background="@android:color/transparent" 
    android:textSize="40sp" 
    android:hint="Title" 
    android:textStyle="bold" 
    android:id="@+id/idEditTextTitle"/> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/idEditTextTitle" 
    android:hint="Content" 
    android:inputType="textMultiLine" 
    android:lines="5" 
    android:gravity="top|left" 
    android:layout_margin="10dp" 
    android:textSize="20sp" 
    android:background="@android:color/transparent" 
    android:id="@+id/idEditTextContent"/> 

<EditText 
    android:layout_width="250dp" 
    android:id="@+id/idEditTextTag" 
    android:layout_height="35dp" 
    android:hint="#TAG" 
    android:background="@android:color/transparent" 
    android:layout_alignParentBottom="true" 
    android:textSize="15dp" 
    android:textStyle="italic" 
    android:layout_margin="10dp"/> 

<android.support.design.widget.FloatingActionButton 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:id="@+id/idFloatingActionButtonSave" 
    android:layout_above="@+id/idFloatingActionButtonEdit" 
    android:layout_margin="20dp" 
    android:visibility="invisible" 
    app:srcCompat="@android:drawable/ic_menu_save" 
    android:layout_alignParentRight="true"/> 

JAVA SCに

package mynotes.pawanjotsingh.com.mynotes.activities; 

import android.content.Intent; 
import android.graphics.Color; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.text.InputType; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Locale; 

import mynotes.pawanjotsingh.com.mynotes.R; 
import mynotes.pawanjotsingh.com.mynotes.adapters.RecyclerAdapter; 
import mynotes.pawanjotsingh.com.mynotes.dbhelper.DataBaseHelper; 
import mynotes.pawanjotsingh.com.mynotes.models.NoteModel; 

public class NoteDetailsActivity extends AppCompatActivity { 

enum Colors{RED, BLUE, GREEN, WHITE} 

DataBaseHelper dataBaseHelper; 
FloatingActionButton floatingActionButtonEdit,floatingActionButtonSave; 
EditText editTextTitle,editTextContent,editTextTag; 
String stringTitle,stringContent,stringTag; 
TextView textViewColors,textViewRed,textViewBlue,textViewGreen; 
Toolbar toolbar; 
int color,colorRed,colorBlue,colorGreen; 

String id=""; 

Colors currentBackgroundColour = Colors.WHITE; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_note_details); 

    editTextTitle=(EditText) findViewById(R.id.idEditTextTitle); 
    editTextTitle.setInputType(InputType.TYPE_NULL); 

    editTextContent=(EditText) findViewById(R.id.idEditTextContent); 
    editTextContent.setInputType(InputType.TYPE_NULL); 

    editTextTag=(EditText) findViewById(R.id.idEditTextTag); 
    editTextTag.setInputType(InputType.TYPE_NULL); 

    toolbar=(Toolbar) findViewById(R.id.idToolbarNoteDetails); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setTitle("Edit"); 

    toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      onBackPressed(); 
     } 
    }); 

    dataBaseHelper=new DataBaseHelper(this); 

    textViewColors=(TextView) findViewById(R.id.idTextViewColors); 
    textViewRed=(TextView) findViewById(R.id.idColorRed); 
    textViewBlue=(TextView) findViewById(R.id.idColorBlue); 
    textViewGreen=(TextView) findViewById(R.id.idColorGreen); 

    colorRed=Color.parseColor("#FE7676"); 
    colorBlue=Color.parseColor("#76FEEC"); 
    colorGreen=Color.parseColor("#99FE76"); 

    stringTitle=getIntent().getStringExtra("text_title"); 
    stringContent=getIntent().getStringExtra("text_content"); 
    stringTag=getIntent().getStringExtra("text_tag"); 
    id = getIntent().getStringExtra("id"); 
    color=getIntent().getIntExtra("color", Color.WHITE); 
    getWindow().getDecorView().setBackgroundColor(color); 


    editTextTitle.setText(stringTitle); 
    editTextContent.setText(stringContent); 
    editTextTag.setText(stringTag); 


    floatingActionButtonSave=(FloatingActionButton) findViewById(R.id.idFloatingActionButtonSave); 
    floatingActionButtonSave.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      save(); 
     } 
    }); 


    floatingActionButtonEdit=(FloatingActionButton) findViewById(R.id.idFloatingActionButtonEdit); 
    floatingActionButtonEdit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      floatingActionButtonSave.setVisibility(View.VISIBLE); 
      Animation animationLeft=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_left); 
      floatingActionButtonSave.startAnimation(animationLeft); 

      textViewColors.setVisibility(View.VISIBLE); 
      Animation animationDown= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_down); 
      textViewColors.startAnimation(animationDown); 

      textViewRed.setVisibility(View.VISIBLE); 
      textViewRed.startAnimation(animationDown); 

      textViewBlue.setVisibility(View.VISIBLE); 
      textViewBlue.startAnimation(animationDown); 

      textViewGreen.setVisibility(View.VISIBLE); 
      textViewGreen.startAnimation(animationDown); 
      editNote(); 


     } 
    }); 

    textViewRed.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      currentBackgroundColour=Colors.RED; 
      getWindow().getDecorView().setBackgroundColor(Color.parseColor("#FE7676")); 
     } 
    }); 

    textViewBlue.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      currentBackgroundColour=Colors.BLUE; 
      getWindow().getDecorView().setBackgroundColor(Color.parseColor("#76FEEC")); 
     } 
    }); 

    textViewGreen.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      currentBackgroundColour = Colors.GREEN; 
      getWindow().getDecorView().setBackgroundColor(Color.parseColor("#99FE76")); 
     } 
    }); 

} 


@Override 
public void onBackPressed() { 
    Intent intent=new Intent(this,MainActivity.class); 
    startActivity(intent); 
} 


public void editNote(){ 


    editTextTitle.setInputType(InputType.TYPE_CLASS_TEXT); 
    editTextTitle.setFocusable(true); 
    editTextTitle.requestFocus(); 

    editTextContent.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE); 


    editTextTag.setInputType(InputType.TYPE_CLASS_TEXT); 



} 

public void save() { 


    int colour=Color.WHITE; 

    switch (currentBackgroundColour){ 
     case RED: 
      colour = colorRed; 
      break; 
     case BLUE: 
      colour = colorBlue; 
      break; 
     case GREEN: 
      colour = colorGreen; 
      break; 
    } 


    final NoteModel noteModel = new NoteModel(); 
    noteModel.setTitle(editTextTitle.getText().toString()); 
    noteModel.setContent(editTextContent.getText().toString()); 
    noteModel.setTag(editTextTag.getText().toString()); 
    noteModel.setId(id); 
    noteModel.setDate(getDateTime()); 
    noteModel.setColor(colour); 

    boolean isModified=dataBaseHelper.editNote(noteModel); 
    if(isModified==true){ 
     Toast.makeText(this, "Modifications saved", Toast.LENGTH_SHORT).show(); 

     finish(); 

    } 
    else Toast.makeText(this, "Unable to save modifications", Toast.LENGTH_SHORT).show(); 

} 

private String getDateTime() { 
    SimpleDateFormat dateFormat = new SimpleDateFormat(
      "dd-MM-yyyy HH:mm:ss", Locale.getDefault()); 
    Date date = new Date(); 
    return dateFormat.format(date); 
} 

リンク:以下

は私のXMLとJavaのコードですreenshots

This image is when I click edit button. This works just fine, text goes to another line.

This image is when i click the save button. The text goes back to single line instead of multi-line.

+0

私はあなたに保証しています:/ 私はその解決策を試してきましたが、私のケースでは役に立たない –

+0

editTextContentに問題がある場合:inputTypeをonCreateの 'InputType.TYPE_NULL'に設定します)そして、後でそれを 'InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE'。あなたが 'InputType.TYPE_NULL'で始まらず、他の入力タイプで始まっていれば、テキストはどのように表示されますか? – 0X0nosugar

+0

'InputType.TYPE_NULL'を削除すると、テキストは複数行になりますが、機能は中断されます。 この行を削除すると、ediTextContentは値を取得できる状態になります。私の目標は、編集ボタンをクリックした後にのみ、editTextContentを値にすることです。 –

答えて

1

わかりましたので、私はこの問題は、私が編集ボタンをクリックしたときに生じることを知っていました。ボタンをクリックすると、InputTypeをXMLのマルチラインとして非常にはっきりと述べたにもかかわらず、自動的にeditTextが一重になりました。

ボタンクリック後にeditTextを複数行にしたのは、私がonClickボタンコードの中にJavaファイルのeditTextContent.setSingleLine(false);を書き込んだことです。 それはそれです。 )

+0

はい、私の回答を受け入れるのに48時間待たなければならないと、6時間後にマークを「チェック」できるようになります。 ありがとう:) –

関連する問題