android TextView setText问题

时间:2014-01-28 16:50:30

标签: java android android-intent textview

我试图在像这样的活动中的TextView上显示一条消息:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    // Get the message from the intent
        Intent intent = getIntent();

        // Create the text view
        TextView textView = (TextView) findViewById(R.id.display_message);      

        if(intent.getExtras() != null){
            String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
            textView.setTextSize(40);
            textView.setText(message);          
        }else{
            textView.setTextSize(40);
            textView.setText(R.string.hello_world);
        }

     // Show the Up button in the action bar.
    setupActionBar();
}

代码应该显示“hello world!”当intent中包含的额外消息为null时,而hello world!则未显示。

enter image description here enter image description here

这是我的strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Action Bar</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
</resources>

发送消息的主要活动:

public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_activity_actions, menu);

    return super.onCreateOptionsMenu(menu);
    //return true;
}

public void sendMessage(View view){
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
    case R.id.action_search:
    //  openSearch();
        return true;
    case R.id.action_settings:
    //  openSettings();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }   
}   
}

3 个答案:

答案 0 :(得分:2)

你应该展示“你好世界!”当intent中包含的额外消息为null时。但是在您的代码中,您只检查意图附加内容。您可能在意图中传递了一些其他参数,因此intent.getExtras()将不会返回null。

你应该尝试一下

if(intent.getExtras() != null && !Textutils(intent.getStringExtra(MainActivity.EXTRA_MESSAGE))){
            String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
            textView.setTextSize(40);
            textView.setText(message);          
        }else{
            textView.setTextSize(40);
            textView.setText(R.string.hello_world);
        }

答案 1 :(得分:2)

您可以检查特定值,无论它是null还是有值。然后按如下所示添加显示逻辑

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    // Get the message from the intent
        Intent intent = getIntent();
        String message = null;

        // Create the text view
        TextView textView = (TextView) findViewById(R.id.display_message);      

        if(intent != null){
            message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        }
        if(message != null && message.length() > 0){
            textView.setTextSize(40);
            textView.setText(message);          
        }else{
            textView.setTextSize(40);
            textView.setText(R.string.hello_world);
        }

     // Show the Up button in the action bar.
    setupActionBar();
}

答案 2 :(得分:1)

intent.getExtras()不返回null。因此,您可以检查intent.getStringExtra(MainActivity.EXTRA_MESSAGE)是否返回""并进行相应的工作。祝你好运!