简体   繁体   中英

How can I add EditText(s) and a CheckBox with a Button?

So I've been working on a small app for myself, as I realized using Microsoft Word to keep track of all the college courses I've completed towards my degree wasn't ideal. So I figured, why not make that my first app? It's simple, and it's doable for someone with no app experience like myself.

So, this was my initial idea, it's nothing special, just some EditText fields along with a CheckBox to mark each course complete. Note, there are actually seven of these, all wrapped in a "ScrollView". 这就是我目前所拥有的。

Then I had a different idea. Instead of having a set amount of courses and credits EditTexts and a CheckBox, I'd rather just have a button to "Add A Course". My problem is, I'm not sure how I would achieve this. I'm still fairly new to programming, only taken two Java classes so far.

Here is my code for my "MainActivity.java":

package exporian.collegetracker;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

    public void onCheckBoxChecked(View v) {
        final EditText course1 = (EditText) findViewById(R.id.course1);
        final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        final EditText credits1 = (EditText) findViewById(R.id.credits1);
        final EditText course2 = (EditText) findViewById(R.id.course2);
        final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        final EditText credits2 = (EditText) findViewById(R.id.credits2);
        final EditText course3 = (EditText) findViewById(R.id.course3);
        final CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
        final EditText credits3 = (EditText) findViewById(R.id.credits3);
        final EditText course4 = (EditText) findViewById(R.id.course4);
        final CheckBox checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
        final EditText credits4 = (EditText) findViewById(R.id.credits4);
        final EditText course5 = (EditText) findViewById(R.id.course5);
        final CheckBox checkBox5 = (CheckBox) findViewById(R.id.checkBox5);
        final EditText credits5 = (EditText) findViewById(R.id.credits5);
        final EditText course6 = (EditText) findViewById(R.id.course6);
        final CheckBox checkBox6 = (CheckBox) findViewById(R.id.checkBox6);
        final EditText credits6 = (EditText) findViewById(R.id.credits6);
        final EditText course7 = (EditText) findViewById(R.id.course7);
        final CheckBox checkBox7 = (CheckBox) findViewById(R.id.checkBox7);
        final EditText credits7 = (EditText) findViewById(R.id.credits7);

        ArrayList<CheckBox> checkBoxes = new ArrayList<>();
        checkBoxes.add(checkBox1);
        checkBoxes.add(checkBox2);
        checkBoxes.add(checkBox3);
        checkBoxes.add(checkBox4);
        checkBoxes.add(checkBox5);
        checkBoxes.add(checkBox6);
        checkBoxes.add(checkBox7);

        ArrayList<EditText> courses = new ArrayList<>();
        courses.add(course1);
        courses.add(course2);
        courses.add(course3);
        courses.add(course4);
        courses.add(course5);
        courses.add(course6);
        courses.add(course7);

        ArrayList<EditText> credits = new ArrayList<>();
        credits.add(credits1);
        credits.add(credits2);
        credits.add(credits3);
        credits.add(credits4);
        credits.add(credits5);
        credits.add(credits6);
        credits.add(credits7);

        int numOfCheckBoxes = 7;
        for (int i = 0; i < numOfCheckBoxes; i++) {
            if (checkBoxes.get(i).isChecked()) {
                courses.get(i).setEnabled(false);
                courses.get(i).setFocusable(false);
                courses.get(i).setInputType(InputType.TYPE_NULL);
                credits.get(i).setEnabled(false);
                credits.get(i).setFocusable(false);
                credits.get(i).setInputType(InputType.TYPE_NULL);
            } else {
                courses.get(i).setEnabled(true);
                courses.get(i).setFocusable(true);
                courses.get(i).setFocusableInTouchMode(true);
                courses.get(i).setInputType(InputType.TYPE_CLASS_TEXT);
                credits.get(i).setEnabled(true);
                credits.get(i).setFocusableInTouchMode(true);
                credits.get(i).setFocusable(true);
                credits.get(i).setInputType(InputType.TYPE_CLASS_NUMBER);
            } // End of if else
        } // End of for loop
    } // End of onCheckBoxChecked
} // End of MainActivity

I realize I've probably gone about this a very "noobish" way, and my code could probably be a lot cleaner, but I'm still fairly new to Java and even newer to Android, so please bear with me.

The biggest thing that I am stuck on is instantiating the EditTexts and CheckBox. How would I go about that? I figured, for my "numOfCheckBoxes" I would just add 1 to it every time the "Add A Course" button is clicked, so the for loop would still work correctly, but how would I create the id's for each new EditText and CheckBox, such as "course1", "course2, "course3", etc.

PS I'm not looking for someone to make the app for me, or for everything to be handed to me, just some insight from someone more experienced than me, thank you all in advance!

Hi first of all you have to create your own class that extends linear layout with horizontal orientation and add to it your sub-views (edit text , text text , check box ), this will allow you to create 1 instance of your view that contains all of the 3 views .

For example :

public class MySingleLineInList extends LinearLayout {

public MySingleLineInList(Context context) {
    super(context);

      setOrientation(LinearLayout.HORIZONTAL);

      EditText courseET = new EditText(context);
      EditText creditsET = new EditText(context);
      CheckBox complteCB = new CheckBox(context);

      addView(courseET);
      addView(creditsET);
      addView(complteCB);
}
}

Then you must use ListView(which is also including scroll) with listview adapter for showing your views(MySingleLineInList), and on button click you can add more views to this list :

ListView myListView = new ListView(context);

MyListArrayAdaptapter myAdapter = new MyListArrayAdaptapter(context);

myListView.setAdapter(myAdapter);



public class MyListArrayAdaptapter extends ArrayAdapter<MySingleLineInList> {

    public MyListArrayAdaptapter(Context context){
          super(context, android.R.layout.simple_spinner_item);
    }

    public void addViewToList(MySingleLineInList l){
        add(l);
        notifyDataSetChanged();
    }
}

Then on button click just use :

Button b = new Button(context);
      b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            myAdapter.addViewToList(new MySingleLineInList());

        }
    });

Please note this is only the concept of doing it , for more information about list view and how it works please read here :

http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92

Or just google for custom listview.

  • about the id's thing you can set id to any view in android for later use, for example :

      MySingleLineInList demo = new MySingleLineInList(context); demo.setId(1); int thisCourseId = demo.getId(); 

You can dynamically create an EditText like this -

    int counter = 1;
    final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    final EditText edit_text = new EditText(this);
    edit_text.setId(counter++);
    edit_text.setLayoutParams(lparams);

And add it to your parent layout.

yourParentLayout.addView(edit_text);

Similarly you can do for CheckBox .

You should use a RecyclerView to show your list of objects. Check out this example.

Then whenever you want to add a new course, just create a new "Course" object and add it to the dataSet array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM