活动更改后,Android Studio TextView消失

时间:2017-02-17 03:37:20

标签: android android-fragments android-activity textview

我已经看过无数的帖子,但似乎无法想出这个。 通过ChangeFont活动更改textview(在主活动中)的字体/颜色/大小后,主活动中的textview似乎消失了。

这是MainActivity.java

package assignment02.csc214.assignment04;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

// keys to pass data in to ChangeFontActivity
static final String KEY_BOLD = "assignment02.csc214.assignment04.BOLD";
static final String KEY_ITALIC = "assignment02.csc214.assignment04.ITALIC";
static final String KEY_UNDERLINE =    "assignment02.csc214.assignment04.UNDERLINED";

static final String KEY_BLUE = "assignment02.csc214.assignment04.BLUE";
static final String KEY_RED = "assignment02.csc214.assignment04.RED";
static final String KEY_YELLOW = "assignment02.csc214.assignment04.YELLOW";

static final String KEY_20SP = "assignment02.csc214.assignment04.20SP";
static final String KEY_30SP = "assignment02.csc214.assignment04.30SP";
static final String KEY_40SP = "assignment02.csc214.assignment04.40SP";

static final String KEY_MESSAGE = "Enter message here.";

private TextView mMainTV;

private boolean mUnderlined = false;

private int mBlue = 0xff0000ff;
private int mRed = 0xffff0000;
private int mYellow = 0xffffff00;

private int m20 = 20;
private int m30 = 30;
private int m40 = 40;

private static final int REQUEST_FONT = 2;
private static final int REQUEST_MESSAGE = 0;

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

    mMainTV = (TextView)findViewById(R.id.tv_main_activity);

    Button fontButton = (Button)findViewById(R.id.fontButton);

    if (savedInstanceState != null){
        String message = savedInstanceState.getString(KEY_MESSAGE);
        mMainTV.setText(message);
    }
    fontButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, ChangeFontActivity.class);

            // passing data in
            // font data
            Typeface typeface = mMainTV.getTypeface();
            intent.putExtra(KEY_BOLD, typeface.isBold());
            intent.putExtra(KEY_ITALIC, typeface.isItalic());
            intent.putExtra(KEY_UNDERLINE, mUnderlined);

            // color data
            intent.putExtra(KEY_BLUE, mBlue);
            intent.putExtra(KEY_RED, mRed);
            intent.putExtra(KEY_YELLOW, mYellow);

            // size data
            intent.putExtra(KEY_20SP, m20);
            intent.putExtra(KEY_30SP, m30);
            intent.putExtra(KEY_40SP, m40);
            startActivityForResult(intent, REQUEST_FONT);
        }
    });
    Button messageButton = (Button)findViewById(R.id.changeMessageButton);
    messageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, ChangeMessageActivity.class);
            intent.putExtra(KEY_MESSAGE, mMainTV.getText().toString());
            startActivityForResult(intent, REQUEST_MESSAGE);
        }
    });
}
@Override
protected void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    // use bundle to save information
    outState.putString(KEY_MESSAGE, mMainTV.getText().toString());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
    super.onRestoreInstanceState(savedInstanceState);
    String message = savedInstanceState.getString(KEY_MESSAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if( resultCode != RESULT_CANCELED ) {
        boolean bold = data.getBooleanExtra(KEY_BOLD, false);
        boolean italic = data.getBooleanExtra(KEY_ITALIC, false);
        boolean underlined = data.getBooleanExtra(KEY_UNDERLINE, false);
        updateFont(bold, italic, underlined);

        boolean blue = data.getBooleanExtra(KEY_BLUE, false);
        boolean red = data.getBooleanExtra(KEY_RED, false);
        boolean yellow = data.getBooleanExtra(KEY_YELLOW, false);
        updateColor(blue, red, yellow);

        boolean text20 = data.getBooleanExtra(KEY_20SP, false);
        boolean text30 = data.getBooleanExtra(KEY_30SP, false);
        boolean text40 = data.getBooleanExtra(KEY_40SP, false);
        updateSize(text20, text30, text40);
    }
    if (resultCode != RESULT_CANCELED){
        CharSequence message = data.getCharSequenceExtra(KEY_MESSAGE);
        mMainTV.setText(message);
    }
}
protected void updateSize(boolean text20, boolean text30, boolean text40){
    if (text20)
        mMainTV.setTextSize(m20);
    else if (text30)
        mMainTV.setTextSize(m30);
    else if (text40)
        mMainTV.setTextSize(m40);

}
protected void updateColor(boolean blue, boolean red, boolean yellow){
    if (blue)
        mMainTV.setTextColor(mBlue);
    else if (red)
        mMainTV.setTextColor(mRed);
    else if (yellow)
        mMainTV.setTextColor(mYellow);
}
protected void updateFont(boolean bold, boolean italic, boolean underline){
    Typeface typeface = mMainTV.getTypeface();
    int style = Typeface.NORMAL;
    if(bold && italic) {
        style = Typeface.BOLD_ITALIC;
    }
    else if(bold) {
        style = Typeface.BOLD;
    }
    else if(italic) {
        style = Typeface.ITALIC;
    }
    Typeface newTypeface = Typeface.create(typeface, style);
    mMainTV.setTypeface(newTypeface);

    if(underline) {
        CharSequence message = mMainTV.getText();
        SpannableString content = new SpannableString(message);
        content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
        mMainTV.setText(content);
        mUnderlined = true;
    }
    else {
        // reset whole message
        mMainTV.setText(mMainTV.getText().toString());
        mUnderlined = false;
    }
  }
}

这是ChangeFontActivity.java

  package assignment02.csc214.assignment04;

  import android.content.Intent;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.CheckBox;

public class ChangeFontActivity extends AppCompatActivity {
      private CheckBox mBoldBox;
      private CheckBox mItalicBox;
      private CheckBox mUnderlineBox;

      private CheckBox mBlue;
      private CheckBox mRed;
      private CheckBox mYellow;

      private CheckBox m20;
      private CheckBox m30;
      private CheckBox m40;

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

    Intent intent = getIntent();

    // get the appropriate checkboxes and set data from MainActivity
    mBoldBox = (CheckBox)findViewById(R.id.check_bold);
    mBoldBox.setChecked(intent.getBooleanExtra(MainActivity.KEY_BOLD, false));

    mItalicBox = (CheckBox)findViewById(R.id.check_italic);
    mItalicBox.setChecked(intent.getBooleanExtra(MainActivity.KEY_ITALIC, false));

    mUnderlineBox = (CheckBox)findViewById(R.id.check_underline);
    mUnderlineBox.setChecked(intent.getBooleanExtra(MainActivity.KEY_UNDERLINE, false));

    mBlue = (CheckBox)findViewById(R.id.color_blue);
    mBlue.setChecked(intent.getBooleanExtra(MainActivity.KEY_BLUE, false));

    mRed = (CheckBox)findViewById(R.id.color_red);
    mRed.setChecked(intent.getBooleanExtra(MainActivity.KEY_RED, false));

    mYellow = (CheckBox)findViewById(R.id.color_yellow);
    mYellow.setChecked(intent.getBooleanExtra(MainActivity.KEY_YELLOW, false));

    m20 = (CheckBox)findViewById(R.id.sp_20);
    m20.setChecked(intent.getBooleanExtra(MainActivity.KEY_20SP, false));

    m30 = (CheckBox)findViewById(R.id.sp_30);
    m30.setChecked(intent.getBooleanExtra(MainActivity.KEY_30SP, false));

    m40 = (CheckBox)findViewById(R.id.sp_40);
    m40.setChecked(intent.getBooleanExtra(MainActivity.KEY_40SP, false));

    Button OKButton = (Button)findViewById(R.id.button_ok);
    OKButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent data = new Intent();
            data.putExtra(MainActivity.KEY_BOLD, mBoldBox.isChecked());
            data.putExtra(MainActivity.KEY_ITALIC, mItalicBox.isChecked());
            data.putExtra(MainActivity.KEY_UNDERLINE, mUnderlineBox.isChecked());

            data.putExtra(MainActivity.KEY_BLUE, mBlue.isChecked());
            data.putExtra(MainActivity.KEY_RED, mRed.isChecked());
            data.putExtra(MainActivity.KEY_YELLOW, mYellow.isChecked());

            data.putExtra(MainActivity.KEY_20SP, m20.isChecked());
            data.putExtra(MainActivity.KEY_30SP, m30.isChecked());
            data.putExtra(MainActivity.KEY_40SP, m40.isChecked());

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

这是ChangeMes​​sageActivity.java

package assignment02.csc214.assignment04;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class ChangeMessageActivity extends Activity implements MyFragment.MessageChangeListener {

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

    MyFragment fragment = new MyFragment();
    fragment.setArguments(getIntent().getExtras());
    getFragmentManager()
            .beginTransaction()
            .replace(R.id.change_message_activity, fragment)
            .commit();
}
@Override
public void messageChanged(CharSequence message) {
    Intent data = new Intent();
    data.putExtra(MainActivity.KEY_MESSAGE, message);
    setResult(RESULT_OK, data);
    finish();
}

@Override
public void messageCanceled() {
    setResult(RESULT_CANCELED);
    finish();
 }
}

最后是MyFragment.java(只使用片段运行changeMes​​sage活动)

package assignment02.csc214.assignment04;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class MyFragment extends Fragment {

public interface MessageChangeListener {
    public void messageChanged(CharSequence message);
    public void messageCanceled();
}
private MessageChangeListener mMessageChangeListener;

public MyFragment() {
    // Required empty public constructor
}
@Override
public void onAttach(Context context) {
    super.onAttach(context);

    mMessageChangeListener = (MessageChangeListener)context;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_my, container, false);

    final EditText editText = (EditText)view.findViewById(R.id.editText);
    editText.setText(getArguments().getString(MainActivity.KEY_MESSAGE));
    Button okButton = (Button)view.findViewById(R.id.activity3_okButton);
    okButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mMessageChangeListener.messageChanged(editText.getText());
        }
    });

    Button cancelButton = (Button)view.findViewById(R.id.activity3_cancelButton);
    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mMessageChangeListener.messageCanceled();
        }
    });
    return view;
  }
}

谢谢。

1 个答案:

答案 0 :(得分:0)

问题是你的onActivityResult()方法正在尝试处理来自两个不同子活动的响应,其中一个活动返回一条消息而另一个不返回,而不区分它们。

您的MainActivity启动两个不同的子活动之一:一个用于更改字体,另一个用于更改消息。当它启动每个活动时,它会提供请求代码:REQUEST_MESSAGEREQUEST_FONT

当子活动返回结果时,请求代码将随之返回。通过这种方式,您可以确定哪个活动返回结果并决定如何处理它。您应该只在请求代码为REQUEST_MESSAGE时更新消息,并且只应在请求代码为REQUEST_FONT时更新字体。

正如所写的那样,只要用户没有按下取消按钮,onActivityResult()总是试图同时做(注意,无论哪个子活动返回结果,你的if语句都将为真)。这意味着即使ChangeFontActivity是返回结果的结果(并且结果不包含消息),您也试图更改显示的消息。消息为null,因此文本将从屏幕“消失”。