TextSwitcher 翻译“文本转换开关”,顾名思义:该转换开关是转换TextView的。开关,意思很明确,必定只有2个文本,一个展示(即“开”),一个隐藏(即“关”)。
2. 简单示例:
java代码:
View rootView = inflater.inflate(R.layout.fragment_main, container, false); final TextSwitcher switcher = (TextSwitcher)rootView.findViewById(R.id.textswitcher); final TextView firsttext = new TextView(PlaceholderFragment.this.getActivity()); firsttext.setId(123); final TextView secondtext = new TextView(PlaceholderFragment.this.getActivity()); secondtext.setId(456); switcher.addView(firsttext, 0); switcher.addView(secondtext,1); Button button = (Button)rootView.findViewById(R.id.myButton); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { int xx = switcher.getCurrentView().getId(); Log.i("textswitcher的当前view的id是",""+xx); if(xx==123){ switcher.setText("123123213"); }else if(xx == 456){ switcher.setText("9999993"); } } }); return rootView; 3.定义布局文件如下:(定义一个textswitcher即可)
<TextSwitcher android:id="@+id/textswitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</TextSwitcher>
原文地址:http://2240844.blog.51cto.com/2230844/1616297