如何在按下按钮后更新从textwatcher返回更新信息的方法

时间:2016-08-15 00:06:46

标签: java android android-edittext textwatcher android-textwatcher

我已声明我的变量,已更改'太空了,以便我可以检查更改edittext时它是否会发生变化,

我认为问题是,当我按下保存按钮或取消按钮时,它将始终产生值null,因为单击按钮时它仍为空。但是,我认为textwatcher会监听EditText,即使EditText中没有任何更改,它默认情况下会将SetChanged()更改为false,因为它提供了"实时更新",但显然这不是&# 39;如果是这样,我做错了吗?或者我应该以不同的方式接近它?,有什么方法可以让它更新吗?

建议将不胜感激。

(PS删除了一些代码以减小尺寸并使其在眼睛上看起来很容易,所以请原谅我没有任何支撑。此外,活动确实在显示布局时正常运行。但是在按下任何按钮时它导致它崩溃。)

public class EditNewItemActivity extends AppCompatActivity{

private Boolean changed = null;
private TextView title,content;
private Button saveBtn,cancelBtn;
private String date;
private int id;


@Override

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

    title = (TextView) findViewById(R.id.editItemTitle);
    content = (TextView) findViewById(R.id.editItemDescription);
    saveBtn = (Button) findViewById(R.id.editItemSaveBtn);
    cancelBtn = (Button) findViewById(R.id.editItemCancelBtn); 

    Bundle extras = getIntent().getExtras();
    title.setText(extras.getString("title"));
    content.setText(extras.getString("content"));

    date = extras.getString("date");
    id = extras.getInt("id");

    GenericTextWatcher textWatcher = new GenericTextWatcher();

    title.addTextChangedListener(textWatcher);
    content.addTextChangedListener(textWatcher);

    ClickEvent clickEvent = new ClickEvent();

    saveBtn.setOnClickListener(clickEvent);
    cancelBtn.setOnClickListener(clickEvent);


}

private class ClickEvent implements View.OnClickListener{

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.editItemSaveBtn:

                save();

                break;

            case R.id.editItemCancelBtn:

                cancel();
                break;
        }
    }
}

private void cancel() {

    if (getChanged() == null){

    //This was used to simply verify that getchanged was still null.
    }

    if (title.getText().toString() != "" || content.getText().toString() != ""){


        if (getChanged() == false) {

 // if nothing has been changed let it cancel etc
        }else {

        }
    }
}

private void save() {

    if (tempTitle != "" || tempContent != "") {

        if(getChanged() == true){
 }

}

public Boolean getChanged() {
    return changed;
}

public void setChanged(Boolean changed) {
    this.changed = changed;
}

private class GenericTextWatcher implements TextWatcher{

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        Log.v("Beforetext:", s.toString());
        EditNewItemActivity editItem = new EditNewItemActivity();

        editItem.setChanged(false);

    }



    @Override
    public void afterTextChanged(Editable s) {

        Log.v("afterTextChanged:", s.toString());

        EditNewItemActivity editItem = new EditNewItemActivity();

        editItem.setChanged(true);

        Log.v("Status:", editItem.getChanged().toString());

    }
}

1 个答案:

答案 0 :(得分:0)

您已更改changed。但是您更改的内容是新EditNewItemActivity,而不是当前页面。

这是您在beforeTextChanged中犯了错误(afterTextChangedGenericTextWatcher)的地方:

EditNewItemActivity editItem = new EditNewItemActivity();
editItem.setChanged(false);   //or true

您应该致电:

setChanged(false);     // or true

实际上,您不应该自己创建一个活动,必须由Android Framework创建活动,以便系统可以管理它。