保存TextView的更改文本

时间:2015-07-08 12:28:21

标签: android sharedpreferences

我在我的第一个活动中有一个Button,它将其背景改为onclick并使用SharedPreferences保存,但我想再设置一个代码,当我检查按钮时,它将我的第二个活动中的TextView文本设置为&#34 ;扣经过"当我取消选中它时,将TextView的文本设置为" Button-Unchecked"然后保存文本(使用SharedPreferences或其他任何东西)。 在我的第一个活动中:

public class MainActivity extends Activity {
private boolean likechek;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button like = (Button) findViewById(R.id.like);
    like.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Dog.this);
            sp.edit().putBoolean("like_", !likechek).commit();
            LikeAnalayzer();
        }
    });

}

    private void LikeAnalayzer() {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    likechek = sp.getBoolean("like_", false);
    UpdateLikeButton();
}

    private void UpdateLikeButton() {
    Button like = (Button) findViewById(R.id.like);

    if(likechek){
        like.setBackgroundResource(R.drawable.starf);
    }else{
        like.setBackgroundResource(R.drawable.star);

    }

}           
}

和第二个(空):

public class Fav extends Activity {
TextView tv ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fav);
    tv = (TextView) findViewById(R.id.tv);
    tv.setText("");
    }
}

感谢您的回答,我真的需要它。

1 个答案:

答案 0 :(得分:0)

如果我理解你正确,你想要在检查按钮的情况下将文本设置为textView,这样你可以像这样更改背景,例如:

private void UpdateLikeButton() {
    Button like = (Button) findViewById(R.id.like);
    TextView myTextView = (TextView)findViewById(R.id.textView);

    if(likechek){
        like.setBackgroundResource(R.drawable.starf);
        myTextView.setText("Button-Checked");
        //and save in the shared preferences the state:
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Dog.this);
        sp.edit().putString("button_state", "Button-Checked").commit();
    }else{
        like.setBackgroundResource(R.drawable.star);
        myTextView.setText("Button-Unchecked");
        //and save in the shared preferences the state:
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Dog.this);
        sp.edit().putString("button_state", "Button-Unchecked").commit();
    }

} 

如果你想在第二个活动中设置textView的文本,只需跳过第一个myTextView.setText("Button-Checked");中的部分,如下所示:

private void UpdateLikeButton() {
    Button like = (Button) findViewById(R.id.like);

    if(likechek){
        like.setBackgroundResource(R.drawable.starf);
        //and save in the shared preferences the state:
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Dog.this);
        sp.edit().putString("button_state", "Button-Checked").commit();
    }else{
        like.setBackgroundResource(R.drawable.star);
        //and save in the shared preferences the state:
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Dog.this);
        sp.edit().putString("button_state", "Button-Unchecked").commit();
    }

} 

并在第二个这样做:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fav);
    tv = (TextView) findViewById(R.id.tv);
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    String buttonState = sp.getString("button_state", "");
    tv.setText(buttonState);
}

希望这有助于你:)