android選中狀態
⑴ android怎麼把checkbox狀態設置為選中狀態
CheckBox和Button一樣,也是一種古老的控制項,它的優點在於,不用用戶去填寫具體的信息,只需輕輕點擊,缺點在於只有「是」和「否」兩種情況,但往往利用它的這個特性,來獲取用戶的一些信息。如一個身份表單中,常常讓用戶填寫「是否已經結婚」,顯然讓用戶去填寫「是」或「否」是不合理的,理想的情景是用如下控制項:
選中後的狀態:
建立checkBox的布局:
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="已婚"
></CheckBox>
顯然,Checked屬性是CheckBox最重要的屬性之一,改變它的方式有三種:
1、XML中申明,在xml布局中指定默認的狀態android:checked="true"。
2、代碼動態改變,在Java中可以直接調用checkbox.setChecked(true);
3、用戶觸摸,即注冊OnCheckedChangeListener事件。
實例如下:
//獲取CheckBox實例
CheckBox
cb
=
(CheckBox)this.findViewById(R.id.cb);
//綁定事件
cb.setOnCheckedChangeListener(new
OnCheckedChangeListener()
{
@Override
public
void
onCheckedChanged(CompoundButton
arg0,
boolean
arg1)
{
//
TODO
Auto-generated
method
stub
Toast.makeText(MyActivity.this,
arg1?"選中了":"取消了選中"
,
Toast.LENGTH_LONG).show();
}
});
在開發當中,默認的狀態是未選中的,如果需要默認選中,通常是在xml中指定即可。
⑵ android中button有幾種狀態
Android中,button按鈕通常有三個狀態:
1. normal(正常狀態);
2. focus(焦點狀態);
3. pressed(按下狀態)
4. selected(選中狀態)
注意:按下後未松開前是pressed,表示按下。
松開後當前項目獲得焦點,是focused。
focused的項只有一個,selected是當選中該按鈕時顯示的狀態。
⑶ android怎麼把checkbox狀態設置為選中狀態
checkBox.setChecked(true); //選中狀態
checkBox.setChecked(false); //未選中狀態
⑷ android怎麼把checkbox狀態設置為選中狀態
您好,很高興能幫助您,
<CheckBox
android:id="@+id/xxx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" //看這里
android:text="@string/xxx"
android:textSize="@dime/xxx" />
你的採納是我前進的動力,還有不懂的地方,請你繼續「追問」!
如你還有別的問題,可另外向我求助;答題不易,互相理解,互相幫助!
⑸ android radiobutton點擊後一直是選中狀態,怎麼再點擊後取消選中
Radiobutton既單選框,多個單選框中必須有一個是選中的,如果你想選擇之後又取消,那麼要使用CheckBox來實現改變它的狀態,方式有三種:
1、XML中申明 android:check="true|false"
2、代碼動態改變 checkBox.setChecked(true|false)
3、用戶觸摸 這個由android系統自動改變
RadioButton使用步驟:
1、RadioButton是圓形單選框
2、RadioGroup是個可以容納多個RadioButton的容器。
3、在RadioGroup中的RadioButton控制項可以有多個,但同時有且僅有一個可以被選中。
代碼如下:
final RadioButton rb_bug = (RadioButton) view.findViewById(R.id.rb_buy);
final GlobalValue globalValue = new GlobalValue();
rb_bug.setOnClickListener(new View.OnClickListener()
{@Overridepublic void onClick(View v) {boolean isCheck = globalValue.isCheck();
if(isCheck{if(v==rb_bug)rb_bug.setChecked(false);}
else{if(v==rb_bug)rb_bug.setChecked(true);}globalValue.setCheck(!isCheck);}});
public class GlobalValue {public boolean isCheck() {return isCheck;}public void setCheck(boolean check)
{isCheck = check;}private boolean isCheck;}
⑹ android listview 中指定checkbox為選中狀態
如果你的listview是自己寫的adapter適配器的話我倒是有一個思路(沒有驗證),點擊按鈕的時候我去更新適配器里的數據信息(主要是需要選中的checkbox的狀態信息),最後調用適配器的dataselectedchange更新數據,以上只是提供一個思路,具體有待驗證
⑺ Android中如何實現高亮顯示即選中狀態
使用工具
操作步驟
聲明layout,並將如下的背景xml設置上去:
⑻ android radiobutton點擊後一直是選中狀態,怎麼再點擊後取消選中
設置一個全局變數為RadioButton的狀態,設置RadioButton點擊監聽事件,監聽你是否點擊按鈕,如果按鈕是點擊狀態,那再次點擊後就會取消選中。
代碼如下:
final RadioButton rb_bug = (RadioButton) view.findViewById(R.id.rb_buy);
final GlobalValue globalValue = new GlobalValue();
rb_bug.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isCheck = globalValue.isCheck();
if(isCheck)
{
if(v==rb_bug)rb_bug.setChecked(false);
}
else
{
if(v==rb_bug)rb_bug.setChecked(true);
}
globalValue.setCheck(!isCheck);
}
});
public class GlobalValue {
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean check) {
isCheck = check;
}
private boolean isCheck;
}
(8)android選中狀態擴展閱讀:
RadioButton使用步驟
1、RadioButton是圓形單選框
2、RadioGroup是個可以容納多個RadioButton的容器。
3、在RadioGroup中的RadioButton控制項可以有多個,但同時有且僅有一個可以被選中。
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請選擇性別" />
<RadioGroup
android:id="@+id/rg_sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rb_Male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/rb_FeMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
</LinearLayout>
網路:RadioButton
⑼ android Listview第一個行默認為選中狀態,怎麼實現按下某一行在沒松開時設置其為選中行,選中行為藍色
1. 你從 A跳轉到B的時候 通過 INTENT 傳一個值 intent.putStringExtart("selection", 4);
2. 然後在B接受這個值: int selection = getExtart().getIntExtart("selection");
3. 然後設置ListView 選擇這個 selection: myListView.setselection(selection);
就OK了
----------- 補充:
剛看太快看錯了, 如果是想選中ListView 某一個 TEXTVIEW, 需要這樣:
ViewGroupitem=(ViewGroup)mAdapter.getChild(selection);
TextViewtextView=item.findviewById(R.id.textView1);
textview.requestFocus();
前提是要設置這個 textview 的 focusable=true, 還有 focusableInTouchmode = true