【问题标题】:Using TextWatcher with Multiple Layout Files将 TextWatcher 与多个布局文件一起使用
【发布时间】:2014-03-31 17:18:05
【问题描述】:

这里是新程序员。我希望屏幕上的“添加”按钮仅在所有字段均非空时启用。但是,添加 TextWatcher 会导致屏幕崩溃。在其他地方询问,似乎我不应该尝试在我的 ContentView 设置为 activity_add_grade 的情况下访问我的 EditText。但我需要帮助来了解正确的做事方式。

我遵循的模板/教程让我为每个活动创建 2 个 xml 文件。如下所示,我的 AddClass 将 contentView 设置为 activity_add_grade,然后膨胀 fragment_add_grade,这是我的屏幕设计和视图所在的位置。所以从 onCreate 访问我的 EditText 视图的问题是我的 ContentView 没有设置为包含视图的 xml。那么我应该在哪里设置我的听众呢?为什么还有两个xml?我想了解更多并遵循最佳实践。谢谢!

AddClass -- 相关活动的 java

public class AddClass extends ActionBarActivity {

DBAdapter db = new DBAdapter(this);

private EditText editText1;
private EditText editText2;

public TextWatcher watcher = new TextWatcher(){
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        checkFieldsForEmptyValues();
    }
    @Override
    public void afterTextChanged(Editable editable) {
    }
};

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
    // setContentView(R.layout.fragment_add_grade); //Tried this. Doesn't work

    editText1 = (EditText)findViewById(R.id.editTitle);
    editText2 = (EditText)findViewById(R.id.editCredit);

    editText1.addTextChangedListener(watcher);
    editText2.addTextChangedListener(watcher);
}

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

    //Code here for spinners on screen. Removed for space
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void checkFieldsForEmptyValues() {

    Button b = (Button)findViewById(R.id.addClassButton);
    System.out.println("in the check fields thing");
    String s1 = editText1.getText().toString();
    String s2 = editText2.getText().toString();

    if(s1.isEmpty() || s2.isEmpty()) {
        b.setEnabled(false);
    } else {
        b.setEnabled(true);
    }

}

// THIS WORKS -- the method is bigger, but I shortened it for space.
public void createNewClass(View v) {
    EditText titleTxt = (EditText)findViewById(R.id.editTitle);
    //ToStrings
    String titleStr = titleTxt.getText().toString();
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    public PlaceholderFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_add_grade,
                container, false);
        return rootView;
    }
}

【问题讨论】:

    标签: java android android-layout android-fragments textwatcher


    【解决方案1】:

    您要查找的视图位于片段的布局中,因此您应该在片段中处理该视图。

    选项 A:

    从您的 Activity onCreate 方法中删除所有与 EditText 相关的代码。在片段内部创建字段观察器和editText1、editText2。所以你的 PlaceholderFragment 代码应该或多或少像这样:

    public static class PlaceholderFragment extends Fragment {
    
        EditText editText1, editText2;
    
        public TextWatcher watcher = new TextWatcher(){
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                checkFieldsForEmptyValues();
            }
            @Override
            public void afterTextChanged(Editable editable) {
            }
        };
    
        public PlaceholderFragment() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
    
            editText1 = (EditText) rootView.findViewById(R.id.editTitle);
            editText1.addTextChangedListener(watcher);
            editText2 = (EditText) rootView.findViewById(R.id.editCredit);
            editText2.addTextChangedListener(watcher);
            return rootView;
        }
    }
    

    选项 B:

    由于您的片段看起来是 eclipse 自动生成的片段,您可能不想在代码中使用片段,您可以这样做: 删除您的片段代码和布局,并将您的片段布局中的编辑文本移动到您的活动布局中。那么你的 Activity 的 onCreate 代码应该可以工作了。

    【讨论】:

    • 谢谢。当我尝试按照建议进行操作时,出现错误,因为我正在尝试使用静态 Fragment 类中的非静态值 (EditText)。使 Fragment 类变为非静态的风险是什么?关于选项 B,有什么理由不这样做吗?我只能猜测自动生成的 Eclipse 代码是根据某种标准。我犹豫要不要偏离。我只是想确保我做的事情是正确的,并且边走边学。再次感谢您的回复。
    • 嗨,我已经更新了代码示例,应该可以工作,虽然我没有测试它,因为我没有你的布局。关于标准:这是一种新标准,使用旧的 android 开发工具不会生成这个占位符片段,只是一个普通的活动。片段的优点是您可以在不同的活动中重用布局片段。在学习 android 时,这可能会令人困惑。我建议reading the official tutorials
    • 谢谢@donfuxx。实际上,在看到您的评论并有所帮助之前,我就开始阅读有关 Fragments 的官方文档。我会将所有与片段相关的代码移动到片段子类中。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2011-12-15
    相关资源
    最近更新 更多