开发者

show the text of an EditText into a TextView

Describing the functionality of my application: I have put in a Relative Layout a TextView, an EditText and a button. All I am trying to do is: when the user writes something in the EditText and push the button, then the content of the EditText is appeared in the TextView(just like a chat-virtual chat). Everything works perfectly, but when the EditText is empty,and the button get pushed, an empty line is appeared in the TextView(and i don't want to..). Although I've tried to solve it using an if the empty line is still appearing in the TextView. I would be really greatfull, if you could help!!! Than you in advance!

Here is my code:

package teiath.android.appliacation;

import android.app.Activity; 
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class M_chat extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /**Code for the scroll bars in the TextView. */
        final TextView tv = (TextView)findViewById(R.id.TEXT_VIEW);
        tv.setMovementMethod(new ScrollingMovementMethod());//for the scroll bars

        /** Code for the scroll bars in the EditText. */
        final EditText wr = (EditText)findViewById(R.id.EDIT_TEXT);
        wr.setMovementMethod(new ScrollingMovementMethod());//for the scroll bars

        final Button button = (Button) findViewById(R.id.btn1);//find the button by id in main.xml
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click


                String wrcontent = wr.getText().toString();//gets the text of the EditText and put it in "tvcontent" variable. 
                String tvcontent = tv.getText().toString();//gets the text of the textView and put it in "tvcontent" variable. 


                if (wrcontent!="")//if the EditText is not empty
                {
                    //check if the TextView is empty o开发者_如何学运维r not
                    if (tvcontent!="")//If it is not empty... 
                    {
                        tv.setText(tvcontent + "\n" + wrcontent);//add its current(TextView's text) text, new line and the text of the EditText as the new text of TextView.
                        //tv.setVisibility(0);//makes visible the textView with the cloud1.png background
                        wr.setText("");//set the text of the Edit Text as empty
                        //wrcontent = "";
                    }
                    else//if the TextView is empty...
                    {
                        tv.setText(wrcontent);//add the text of the editText as the new text of the TextView
                        wr.setText("");
                    }
                }
                /**if (wrcontent=="")
                {

                }*/
                //finish();
            }
        });


    }
}


Don't use !="" for String comparison. To check for empty text, use something like

if ( wrcontent != null && wrcontent.trim().length() == 0 ) {

Better yet, include Guava libraries in your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜