ユーザーがオプションとしてボタンの1つを選択できるボタンのグループが必要です。それは振る舞いのようなラジオボタングループでなければなりませんが、私はラジオサークルが存在することを望んでいません。ユーザーがボタンの1つだけを切り替えることができるようにしたいだけです。
トグルグループのようなものが必要だと思います。
このようなものはAndroidに存在しますか?
ユーザーがオプションとしてボタンの1つを選択できるボタンのグループが必要です。それは振る舞いのようなラジオボタングループでなければなりませんが、私はラジオサークルが存在することを望んでいません。ユーザーがボタンの1つだけを切り替えることができるようにしたいだけです。
トグルグループのようなものが必要だと思います。
このようなものはAndroidに存在しますか?
私はそのようなものを再利用したいと思いますRadioGroup
:(onClick
属性に注意してください。つまり、ボタンをクリックするとアクティビティのonToggle(View)
メソッドがトリガーされます。
<RadioGroup android:id="@+id/toggleGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:orientation="horizontal"
>
<ToggleButton android:id="@+id/btn_Letter"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textSize="14sp"
android:textOn="Letter"
android:textOff="Letter"
android:onClick="onToggle"
android:checked="true"
/>
<ToggleButton android:id="@+id/btn_A4"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textSize="14sp"
android:textOn="A4"
android:textOff="A4"
android:onClick="onToggle"
/>
</RadioGroup>
アクティビティまたは他の場所で、リスナーを定義できます。
static final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
for (int j = 0; j < radioGroup.getChildCount(); j++) {
final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
view.setChecked(view.getId() == i);
}
}
};
そしてそれを登録します、例えばonCreate()
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.scan_settings);
((RadioGroup) findViewById(R.id.toggleGroup)).setOnCheckedChangeListener(ToggleListener);
}
最後にonToggle(View)
、アプリに固有の、必要なことは何でもします。また、切り替えられたビューのIDを使用してRadioGroupのcheckメソッドを呼び出します。そのようです:
public void onToggle(View view) {
((RadioGroup)view.getParent()).check(view.getId());
// app specific stuff ..
}
通常のラジオボタンを使用し、ラジオボタンの背景に画像を使用できます。テキスト文字列は指定しないでください。
<RadioButton
android:id="@+id/custom_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:background="@drawable/button_custom"
/>
背景には、任意のドローアブルを使用しますが、ほとんどの場合、セレクターを使用して、さまざまな状態にさまざまな画像を提供できるようにする必要があります。最も単純なバージョンでは、2つの画像のみを使用します。
<item android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/custom_selected" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="@drawable/custom_normal" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/custom_selected" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/custom_normal" />
<item android:state_checked="false" android:drawable="@drawable/custom_normal" />
<item android:state_checked="true" android:drawable="@drawable/custom_selected" />
これにより、ラジオボタンは通常のボタンのように見え(つまり、提供したドローアブルのように見えます)、ラジオグループのラジオボタンのように動作します。
上記で概説したすべての方法を試しましたが、実際にうまく機能した方法はありませんでした。
ラジオボタンをハックして実際のボタンのように見せようとすると、見栄えが悪くなります。
最終的に、RadioGroupソースコードを取得し、RadioButtonではなくToggleButtonを受け入れるように変更しました。本当にうまくいきます!
Githubのソースは次のとおりです:ToggleGroup
使用法:
<com.rapsacnz.ToggleGroup
android:id="@+id/deal_detail_toolbar"
android:layout_width="fill_parent"
android:layout_height="60dip"
android:layout_alignParentBottom="true"
android:background="@drawable/bgnd_toggle_button">
<ToggleButton
android:id="@+id/b1"
android:textOn="@string/tab_1_label"
android:textOff="@string/tab_1_label"
android:textSize="10sp"
android:textColor="@color/tab_button_color"
android:checked="true"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_weight="1"
android:drawableTop="@drawable/toggle_spotlight"
android:drawablePadding="-5dp "
android:gravity="bottom|center"
android:paddingTop="10dp"
android:paddingBottom="5dp"
android:background="@drawable/bgnd_transparent" />
<ToggleButton
android:id="@+id/b2"
android:textOn="@string/tab_2_label"
android:textOff="@string/tab_2_label"
android:textSize="10sp"
android:textColor="@color/tab_button_color"
android:checked="false"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_weight="1"
android:drawableTop="@drawable/toggle_browse"
android:gravity="bottom|center"
android:paddingTop="10dp"
android:paddingBottom="5dp"
android:background="@drawable/bgnd_transparent" />
<ToggleButton
android:id="@+id/b3"
android:textOn="@string/tab_3_label"
android:textOff="@string/tab_3_label"
android:textSize="10sp"
android:textColor="@color/tab_button_color"
android:checked="false"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_weight="1"
android:drawableTop="@drawable/toggle_purchased"
android:gravity="bottom|center"
android:paddingTop="10dp"
android:paddingBottom="5dp"
android:background="@drawable/bgnd_transparent" />
</com.rapsacnz.ToggleGroup>
お役に立てれば
これが私がそれをどうやってやったかです(RadioGroup
関与していません):
private CompoundButton.OnCheckedChangeListener toggleListener = new CompoundButton.OnCheckedChangeListener()
{
boolean avoidRecursions = false;
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(avoidRecursions) return;
avoidRecursions = true;
// don't allow the un-checking
if(!isChecked)
{
buttonView.setChecked(true);
avoidRecursions = false;
return;
}
// un-check the previous checked button
if(buttonView != toggleButton1 && toggleButton1.isChecked()) toggleButton1.setChecked(false);
else if(buttonView != toggleButton2 && toggleButton2.isChecked()) toggleButton2.setChecked(false);
else if(buttonView != toggleButton3 && toggleButton3.isChecked()) toggleButton3.setChecked(false);
else if(buttonView != toggleButton4 && toggleButton4.isChecked()) toggleButton4.setChecked(false);
avoidRecursions = false;
}
};
// ...
toggleButton1.setOnCheckedChangeListener(toggleListener);
toggleButton2.setOnCheckedChangeListener(toggleListener);
toggleButton3.setOnCheckedChangeListener(toggleListener);
toggleButton4.setOnCheckedChangeListener(toggleListener);
Kotlinを使用していて、Wolf Paulusの回答を試しましたが、うまくいきませんでした。私はそれをいじって、次の方法でそれを機能させることができました:最初に、xmlから「onClick」を削除しました:
<RadioGroup android:id="@+id/toggleGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:orientation="horizontal"
>
<ToggleButton android:id="@+id/btn_Letter"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textSize="14sp"
android:textOn="Letter"
android:textOff="Letter"
android:checked="true"
/>
<ToggleButton android:id="@+id/btn_A4"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textSize="14sp"
android:textOn="A4"
android:textOff="A4"
/>
</RadioGroup>
ToggleListener
私は代わりに、私が持っているonCreate()
それぞれのを聞いた中で、を使用しませんでしたToggleButton
:
btn_Letter.setOnClickListener { it -> onToggle(it) }
btn_A4.setOnClickListener { it -> onToggle(it) }
そして最も重要な部分は、onToggle(btn: View)
private fun onToggle(btn: View) {
val radioGroup = (btn.parent as RadioGroup)
for (index in 0 until radioGroup.childCount) {
val child = radioGroup.getChildAt(index) as ToggleButton
child.isChecked = child.id == btn.id
// do what ever else you need to do
}
}
これだよ。これがお役に立てば幸いです。
私のソリューションは同じ効果を達成しますが、ラジオボタンを使用しません。
xmlの場合、最初に「@ drawable/myselectorfile」の「selector」を解除します。
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@drawable/icon_off" />
<item android:state_checked="true"
android:drawable="@drawable/icon_on" />
</selector>
リストビューのアイテムのファイルを削除します。
<?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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtInfo"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:paddingTop="15dip" />
<ToggleButton
android:id="@+id/BtnToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:background="@drawable/toggle_style"
android:padding="10dip"
android:textOff=""
android:textOn=""
android:focusable="false"/>
</LinearLayout>
と私のリストビュー:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lv_lista"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
oncreateメソッドで:
lista.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onItemClick(AdapterView<?> pariente, View view, int posicion, long id) {
@SuppressWarnings("unused")
ListEntity elegido = (ListEntity) pariente.getItemAtPosition(posicion);
varToggle = (ToggleButton) view.findViewById(R.id.BtnToggle);
varToggle.setChecked(true);
// showDialog(0); // dialog optional
//restart btn's
reStart(posicion);
}
});
Activityクラス:
@Override
protected Dialog onCreateDialog(int id){
Dialog dialogo = crearDialogoConfirmacion();
return dialogo;
}
public void reStart(int posicion){
for(int j=0;j<lista.getCount();j++)
{
View vista = lista.getChildAt(j);
ToggleButton varToggle2 = (ToggleButton) vista.findViewById(R.id.BtnToggle);
if(j != posicion){ varToggle2.setChecked(false); }
}
}
私はButterKnifeを使用して、RadioGroupの代わりにLinearLayoutでそれを行いました。
@BindViews({R.id.one, R.id.two, R.id.three})
List<ToggleButton> toggleGroup;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
ButterKnife.bind(this, view);
}
@OnClick({R.id.one, R.id.two, R.id.three})
public void toggle(ToggleButton selected) {
if (selected.isChecked()) {
// Deselect other buttons
for (ToggleButton button : toggleGroup) {
if (button.getId() != selected.getId()) {
button.setChecked(false);
}
}
} else {
// Disallow deselecting the current button
selected.toggle();
}
}
では、一度に1つしか選択できないボタンが必要ですか?ラジオボタンを表示しない場合、ユーザーは自分が何を選択したかをどのようにして知ることができますか?
RadioButtonとRadioGroupを使用できますが、アプリにより適した外観のカスタムラジオボタンを簡単に生成できるかどうかはわかりません。しかし、ラジオボタン自体を拡張して描画メソッドをオーバーライドしたり、ラジオグループに統合できるボタンの子を調べたりできれば、どういうわけか可能になるはずです。ビューに特別なインターフェイスを実装してから、カスタムボタンのいくつかを1つのラジオグループに結合できるかもしれません。
任意のコンテナViewGroupを使用して、CompoundButtons(CheckBox、RadioButton、Switch、SwitchCompat、ToggleButton)を追加し、onCheckedChanged内で ユーティリティ関数を呼び出して他のボタンをクリアします。
<LinearLayout
android:id="@+id/toggle_group"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ToggleButton
android:id="@+id/toggle_btn1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textOff="Off1"
android:textOn="On1" />
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_margin ="8dp"
android:background="#ffff" />
<ToggleButton
android:id="@+id/toggle_btn2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textOff="Off2"
android:textOn="On2" />
</LinearLayout>
onCheckedChanged内で、状態の変更を処理し、ユーティリティメソッドを呼び出して他の子をクリアします。以下は、 ViewGroupから派生したすべてのビューコンテナグループをサポートし、コンテナ内に非CompoundButtonオブジェクトを混在させることができます。多くの場合、すでにonCheckedChangedコールバックがあるため、ユーティリティ関数を呼び出すための新しいコードのみがあります。
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
switch (id) {
case R.id.toggle_btn1:
if (isChecked) {
doBtn1Action();
doRadioGroupChange((ViewGroup)buttonView.getParent(), id);
}
break;
case R.id.toggle_btn2:
if (isChecked) {
doBtn2Action();;
doRadioGroupChange((ViewGroup)buttonView.getParent(), id);
}
break;
そして、これが子をクリアするためのユーティリティ関数です。
/**
* Emulate radioGroup and clear buttons which don't match checkedId.
* @param viewGroup
* @param checkedId
*/
private static void doRadioGroupChange(final ViewGroup viewGroup, final int checkedId) {
for (int rgIdx = 0; rgIdx < viewGroup.getChildCount(); rgIdx++) {
View child = viewGroup.getChildAt(rgIdx);
if (child instanceof CompoundButton) {
final CompoundButton view = (CompoundButton) viewGroup.getChildAt(rgIdx);
view.setChecked(view.getId() == checkedId);
}
}
}
public void OnTabSelected(View v){
RadioGroup radioGroup = (RadioGroup)v.getParent();
for (int j = 0; j < radioGroup.getChildCount(); j++) {
ToggleButton toggleButton = (ToggleButton) radioGroup.getChildAt(j);
toggleButton.setChecked(v.getId() == toggleButton.getId());
}
}
Kotlinバージョン:
for(index in 0..(radioGroup.childCount-1))
(radioGroup.getChildAt(index) as ToggleButton).isChecked = false