connected articles: Talk android developed menu (a)
Version two:
version of a raw material is LinearLayout + Buttons + LayoutInflater. we can think of, "exclusive" This property obviously endorsement by the RadioButton. So this edition we will RadioButton join. Remained the same layout files, modify and add the next RadioButton RadioGroup on OK. Logical aspects, the need to achieve OnCheckedChangeListener interface. Meanwhile, let's change the background image used here selector instead. Layout of the code is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<RadioGroup
android:id="@+id/rb_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_0"
android:checked="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:button="@null"
android:layout_weight="1"
android:background="@drawable/tab_index_selector"/>
<RadioButton
android:id="@+id/rb_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:button="@null"
android:layout_weight="1"
android:background="@drawable/tab_home_selector"/>
<RadioButton
android:id="@+id/rb_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:button="@null"
android:layout_weight="1"
android:background="@drawable/tab_wt_selector"/>
<RadioButton
android:id="@+id/rb_3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:button="@null"
android:layout_weight="1"
android:background="@drawable/tab_yh_selector"/>
</RadioGroup>
</LinearLayout>
</TabHost>
selector drawable needs to achieve an xml file, the code is as follows:
<?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/bst_tab_home_0" />
<item android:state_checked="true" android:drawable="@drawable/bst_tab_home_1" />
</selector>
in onCheckedChanged, the point to note is that, Do not forget to determine whether the current RadioButton was clicked . Because onCheckedChanged record is to click on a state change RadioButton, so every time there will be two RadioButton click event changes state, respectively, by selected to unselected from unchecked to checked. So if you only need to get the RadioButton is clicked, it is recommended in the code plus one if the outermost judgment. Code is as follows:
package com.example.menutest;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
public class MenuRbActivity extends Activity implements OnCheckedChangeListener{
private RadioGroup radioGroup;
private RadioButton rbHd,rbHome,rbWt,rbYh;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rb);
initViews();
}
private void initViews() {
// TODO Auto-generated method stub
//Initialize RadioGroup and RadioButtons
radioGroup = (RadioGroup) findViewById(R.id.rb_group);
rbHd = (RadioButton) findViewById(R.id.rb_0);
rbHd.setOnCheckedChangeListener(this);
rbHome = (RadioButton) findViewById(R.id.rb_1);
rbHome.setOnCheckedChangeListener(this);
rbWt = (RadioButton) findViewById(R.id.rb_2);
rbWt.setOnCheckedChangeListener(this);
rbYh = (RadioButton) findViewById(R.id.rb_3);
rbYh.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton bv, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
switch(bv.getId()){
case R.id.rb_0:
rbHd.setChecked(true);
//设置第一个选项的内容break;
case R.id.rb_1:
rbHd.setChecked(true);
//设置第二个选项的内容break;
case R.id.rb_2:
rbHd.setChecked(true);break;
case R.id.rb_3:
rbHd.setChecked(true);break;
}
}
}
version two options district was changed. Then the content area still choose to use LayoutInflater.
version three:
version we have completed more than two options for zone change, use the RadioButton has been relatively close now most programmers develop habits.
then the content area, although LayoutInflater can be relatively simple to achieve View the paste, but on the code, the more redundant and complicated. At this time, a familiar old friend debut, TabHost..
TabHost realization and the first two versions of the LayoutInflater almost. The difference is that, TabHost View each content layout are written in either the same file - so you do not need LayoutInflater, I guess the bottom using a similar findViewById () method. ( know a friend please say something. ) either is to write each content View the layout of the document in a different layout, then this is the use of LayoutInflater .
addition, TabHost benefit is, google these packages up (you can not see LayoutInflater). So the code on the structural strength, can be ornamental improved.
RadioButton + TabHost Here is the code:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<RadioGroup
android:id="@+id/rb_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_0"
android:checked="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:button="@null"
android:layout_weight="1"
android:background="@drawable/tab_index_selector"/>
<RadioButton
android:id="@+id/rb_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:button="@null"
android:layout_weight="1"
android:background="@drawable/tab_home_selector"/>
<RadioButton
android:id="@+id/rb_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:button="@null"
android:layout_weight="1"
android:background="@drawable/tab_wt_selector"/>
<RadioButton
android:id="@+id/rb_3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:button="@null"
android:layout_weight="1"
android:background="@drawable/tab_yh_selector"/>
</RadioGroup>
</TabWidget>
</LinearLayout>
</TabHost>
layout file is added to the TabHost, to note is that if you inherit from TabActivity, then use the default setContentView (), we must have tabhost, tabcontent, tabs, etc. exist, respectively, the corresponding components. Id set when @ android: id / tabcontent, this is because these are the android itself owned by id, no + id.
package com.example.menutest;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;
public class MenuRbActivity extends TabActivity implements OnCheckedChangeListener{
private RadioButton rbHd,rbHome,rbWt,rbYh;
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rb);
initViews();
}
private void initViews() {
// TODO Auto-generated method stub
//Initialize RadioGroup and RadioButtons
rbHd = (RadioButton) findViewById(R.id.rb_0);
rbHd.setOnCheckedChangeListener(this);
rbHome = (RadioButton) findViewById(R.id.rb_1);
rbHome.setOnCheckedChangeListener(this);
rbWt = (RadioButton) findViewById(R.id.rb_2);
rbWt.setOnCheckedChangeListener(this);
rbYh = (RadioButton) findViewById(R.id.rb_3);
rbYh.setOnCheckedChangeListener(this);
//Initialize TabHost
mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("0").setIndicator("").setContent(new Intent(MenuRbActivity.this,Tab0Activity.class)));
mTabHost.addTab(mTabHost.newTabSpec("1").setIndicator("").setContent(new Intent(MenuRbActivity.this,Tab1Activity.class)));
mTabHost.addTab(mTabHost.newTabSpec("2").setIndicator("").setContent(new Intent(MenuRbActivity.this,Tab2Activity.class)));
mTabHost.addTab(mTabHost.newTabSpec("3").setIndicator("").setContent(new Intent(MenuRbActivity.this,Tab3Activity.class)));
//let view tab0 be showed at first
mTabHost.setCurrentTab(0);
}
@Override
public void onCheckedChanged(CompoundButton bv, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
switch(bv.getId()){
case R.id.rb_0:
rbHd.setChecked(true);
mTabHost.setCurrentTab(0);
break;
case R.id.rb_1:
rbHd.setChecked(true);
mTabHost.setCurrentTab(1);
break;
case R.id.rb_2:
rbHd.setChecked(true);
mTabHost.setCurrentTab(2);
break;
case R.id.rb_3:
rbHd.setChecked(true);
mTabHost.setCurrentTab(3);
break;
}
}
}
logic is the basic version of two of the content.
version three basic tab to complete the general framework. RadioButton + TabHost also a more widespread use of menus. Can we add to this menu slide switch View it? This have to wait until the next version to speak.
Hard to say what causes the problem.
回复删除