如何更改它以便显示复选框而不是文本?

时间:2015-10-05 19:51:38

标签: javascript java android search checkbox

如何更改它以便显示复选框而不是文本? 现在用户可以搜索加拿大,该应用程序向用户显示文本加拿大。 现在我想,但我可以找到文本复选框。

这是如何运作的?

非常感谢!

最诚挚的问候, 费边

的.java



public class Home extends AppCompatActivity {

    String[] items;
    ArrayList<String> listItems;
    ArrayAdapter<String> adapter;
    ListView listView;
    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        listView=(ListView)findViewById(R.id.listview);
        editText=(EditText)findViewById(R.id.txtsearch);
        initList();
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.toString().equals("")){
                    // reset listview (resette listview)
                    initList();
                }
                else{
                    // perform search
                    searchItem(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }


    public void searchItem(String textToSearch){
        for(String item:items){
            if(!item.contains(textToSearch)) {
                listItems.remove(item);
            }

        }
        adapter.notifyDataSetChanged();

    }

    public void initList(){
        items=new String[]{"Canada","China"};
        listItems=new ArrayList<>(Arrays.asList(items));
        adapter=new ArrayAdapter<String>(this,R.layout.list_item, R.id.txtitem, listItems);
        listView.setAdapter(adapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home, menu);
        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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
&#13;
&#13;
&#13;

.XML

&#13;
&#13;
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Text"
        android:id="@+id/textview"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Buttontext"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />



    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtsearch"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:hint="search"
        />
        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></ListView>




</RelativeLayout>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

    you should add or change <CheckBox> according to your requirement for the output 
    like

    <!----Your java class---->
    <CheckBox android:id="@+id/checkbox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text"
            android:onClick="onCheckboxClicked"/>

    <!----Your java class---->
     public class Home extends AppCompatActivity {

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

             final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
             if (checkBox.isChecked()) {
                 checkBox.setChecked(false);
//Rest of your code
             }
         }
     }

答案 1 :(得分:0)

这取决于您希望应用程序的外观,但为了创建一个复选框列表,您可以简单地使用包含这些复选框的垂直方向的LinearLayout。

实施取决于您的数据源是静态还是动态。

<强>静态: 如果你想创建一个静态的复选框列表,你可以简单地将它们添加到LinearLayout(如下所示),然后将这个LinearLayout添加到你的主布局(而不是ListView):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/layout_cbx" >

    <CheckBox
        android:id="@+id/cbx_canada"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Canada" />

    <CheckBox
        android:id="@+id/cbx_china"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="China"
        android:checked="true" />
</LinearLayout>

<强>动态: 如果您希望动态创建复选框(这是常见情况),请首先创建一个LinearLayout作为容器:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/layout_cbx" />

然后在活动中添加复选框(例如,在initList之后将以下内容添加到onCreate):

LinearLayout layout = (LinearLayout)findViewById(R.id.layout_cbx);
        for(String item : items){
            CheckBox cbx = new CheckBox(this);
            cbx.setText(item);
            layout.addView(cbx);
        }

修改 当您使用静态资源时,您的searchItem方法应该为每个复选框包含类似的内容:

CheckBox cbx = (CheckBox)findViewById(R.id.cbx_china);
cbx.setChecked(textToSearch == cbx.getText());
cbx = (CheckBox)findViewById(R.id.cbx_canada);
cbx.setChecked(textToSearch == cbx.getText());

同样,在我看来,这不是最好的做法。

答案 2 :(得分:0)

我应该准确改变什么? 请在代码中填写此内容并删除未使用的代码:

CheckBox cbx = (CheckBox)findViewById(R.id.cbx_china);
if (textToSearch == cbx.getText())
cbx.setChecked(!cbx.isChecked());

我很抱歉,但此刻我很困惑。

public class Home extends AppCompatActivity {

    String[] items;
    ArrayList<String> listItems;
    ArrayAdapter<String> adapter;
    ListView listView;
    EditText editText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
// CheckBox
        final CheckBox checkBox = (CheckBox) findViewById(R.id.cbx_canada);
        if (checkBox.isChecked()) {
            checkBox.setChecked(false);
            

            //listView = (ListView) findViewById(R.id.listview);
            editText = (EditText) findViewById(R.id.txtsearch);
            initList();
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.toString().equals("")) {
                        // reset listview (resette listview)
                        initList();
                    } else {
                        // perform search
                        searchItem(s.toString());
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

        }}


    public void searchItem(String textToSearch){
        for(String item:items){
            if(!item.contains(textToSearch)) {
                listItems.remove(item);
            }

        }
        adapter.notifyDataSetChanged();

    }

    public void initList(){
        items=new String[]{"Canada","China"};//////////////////////////////////////
        listItems=new ArrayList<>(Arrays.asList(items));
        adapter=new ArrayAdapter<String>(this,R.layout.activity_home, R.id.activity_home, listItems); // List item dazugefügt
        listView.setAdapter(adapter);
    }

    // Menüs //

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home, menu);
        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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

非常感谢! 最好的祝福, 费边