android if语句不起作用

时间:2016-07-23 10:21:04

标签: android if-statement

public class MainActivity extends Activity 
{

    public static String EXTRA_MESSAGE;

    private Intent ceec;
    private  EditText cc;

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


    public void sendMessage(View view) 
    {
        ceec = new Intent(this, ToActivity.class);

        cc = (EditText) findViewById(R.id.edit_message);


        String message = cc.getText().toString();
        ceec.putExtra(EXTRA_MESSAGE, message);
        startActivity(ceec);
}}

public class ToActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);



        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);

        textView.setText(message);
        textView.setTextColor(Color.rgb(5,8,100));


        if( message == "hi"){

            textView.setTextSize(80);
        }


        // Set the text view as the activity layout
        setContentView(textView);
        }}


[       if( message == "hi"){

            textView.setTextSize(80);
        } ]

为什么不起作用?以及如何解决它并谢谢你

2 个答案:

答案 0 :(得分:2)

而不是

message == "hi"

你应该这样做:

message.equal("hi") 

永远不要将字符串与==进行比较,请查看this question以了解原因。

答案 1 :(得分:0)

使用.equals()方法,

if(message.equals("hi")){

    textView.setTextSize(80);
}
else
{
   // do your else stuff
}

这应该有效。