android常用控件【JAVA】
来源:互联网 发布:变频布林线源码 编辑:程序博客网 时间:2024/06/04 01:38
一.ToggleButton按钮控件
事例:用一个ToggleButton按钮控制灯泡的开关,即转换图片
代码:
1.activity_main.xml
<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" android:orientation="vertical"> <ToggleButton android:id="@+id/toggleButton1" android:checked="false" android:textOn="开" android:textOff="关" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/off" /></LinearLayout>
2.MainActivity.java
package com.example.togglebuttontest;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.CompoundButton;import android.widget.ImageView;import android.widget.ToggleButton;public class MainActivity extends Activity implements OnCheckedChangeListener{private ToggleButton tb;private ImageView img;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tb=(ToggleButton) findViewById(R.id.toggleButton1);img=(ImageView) findViewById(R.id.imageView1);tb.setOnCheckedChangeListener(this);}@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// 当tb被点击时执行img.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off);}}需要用到的图片:
二.AutoCompleteTextView和MultiAutoCompleteTextView控件
事例:输入3个字符即实现文本匹配
1.activity_main.xml
<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" android:orientation="vertical"> <AutoCompleteTextView android:completionThreshold="3" android:id="@+id/autoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/hint_name" > </AutoCompleteTextView> <MultiAutoCompleteTextView android:id="@+id/multiAutoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/hint_name2" > </MultiAutoCompleteTextView></LinearLayout>
2.MainActivity.java
package com.example.second;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;import android.widget.MultiAutoCompleteTextView;public class MainActivity extends Activity {private AutoCompleteTextView acTextView;private String[] res={"beijing1","beijing2","beijing3","shanghai1","shanghai2"};private MultiAutoCompleteTextView macTextView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);acTextView=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,res);acTextView.setAdapter(adapter);macTextView=(MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);macTextView.setAdapter(adapter);macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());}}
三.MarqueeText即走马灯效果
事例:两个TextView当每个字数很多时能够实现走马灯效果
1.activity_main.xml
<RelativeLayout 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" tools:context="${relativePackage}.${activityClass}" > <com.example.marqueentextviewdemo.MarqueeText android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:textColor="#069" android:focusableInTouchMode="true" android:text="@string/hello_world" /> <com.example.marqueentextviewdemo.MarqueeText android:layout_below="@id/textview1" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textColor="#ababab" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:text="@string/hello_world" /></RelativeLayout>
2.MarqueeText.java
package com.example.marqueentextviewdemo;import android.content.Context;import android.util.AttributeSet;import android.widget.TextView;public class MarqueeText extends TextView {public MarqueeText(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public MarqueeText(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}public MarqueeText(Context context) {super(context);// TODO Auto-generated constructor stub}@Overridepublic boolean isFocused() {return true;}}
四.单选框RadioGroup及复选框CheckBox以及Button按钮
1.activity_main.xml
<LinearLayout xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:text="游泳" /> <RadioGroup android:id="@+id/radioGroup1" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="男" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup> <Button android:id="@+id/button1" android:layout_weight="3" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button2" android:layout_weight="5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /></LinearLayout>
2.MainActivity.java
package com.example.checkboxdemo;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.RadioGroup;public class MainActivity extends Activity implements android.widget.RadioGroup.OnCheckedChangeListener{private CheckBox cb;private RadioGroup rg;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);cb=(CheckBox) findViewById(R.id.checkBox1);cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubLog.i("tag", isChecked+" ");if(isChecked){String text=cb.getText().toString();Log.i("tag", text);}}});rg=(RadioGroup) findViewById(R.id.radioGroup1);rg.setOnCheckedChangeListener(this);}@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch(checkedId){case R.id.radio0:Log.i("tag", "你当前是一个男孩");break;case R.id.radio1:Log.i("tag", "你当前是一个女孩");break;}}}
3.tabletest.xml
<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:stretchColumns="*" android:layout_height="match_parent" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:layout_column="1" android:id="@+id/button2" android:layout_span="2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </TableRow></TableLayout>
五.ProgressBar进度条
1.pro.xml
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ProgressBar android:id="@+id/progressBar1" android:layout_gravity="center" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_gravity="center" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="80%" /></FrameLayout>
六.页面跳转
1.factivity.xml
<?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" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第一种启动方式" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二种启动方式" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="把第二个页面回传的数据显示出来" /></LinearLayout>
2.sactivity.xml
<?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" > <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /></LinearLayout>3.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.intentdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > </activity> <activity android:name=".FActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SActivity" android:label="@string/app_name" > </activity> </application></manifest>
4.FActivity.java
package com.example.intentdemo;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class FActivity extends Activity{private Button bt1;private Button bt2;private Context cmtext;private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.factivity);bt1=(Button) findViewById(R.id.button1);tv=(TextView) findViewById(R.id.textView1);cmtext=this;bt1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent=new Intent(FActivity.this, SActivity.class);startActivity(intent);}});//通过startActivityForResultbt2=(Button) findViewById(R.id.button2);bt2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent=new Intent(cmtext, SActivity.class);startActivityForResult(intent, 1);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if(requestCode==1&&resultCode==2){String content=data.getStringExtra("data");tv.setText(content);}}}
5.SActivity.java
package com.example.intentdemo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class SActivity extends Activity{private Button ton1;private String context="你好";@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.sactivity);ton1=(Button) findViewById(R.id.button3);ton1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent data=new Intent();data.putExtra("data", context);setResult(2,data);finish();}});}}
0 0
- android常用控件【JAVA】
- android控件与常用控件
- android 常用控件 时钟控件
- android控件与常用控件
- Android 常用控件讲解 --控件
- Android常用控件
- Android常用控件-spinner
- Android常用控件-AutoCompleteTextView
- android常用控件(转)
- android 的常用控件
- Android常用控件总结
- Android常用控件总结
- Android控件常用属性
- Android常用控件总结
- Android常用控件总结
- Android控件常用属性
- Android常用控件总结
- android常用控件属性
- RecycleView添加HeaderView宽度不能撑满,第一次加载HeaderView的子控件获取不到焦点
- Android原生SQLite操作以及greenDao框架操作SQLite
- 51NOD 1068 Bash游戏 V3
- 逆波兰计算器
- linux文件目录权限管理
- android常用控件【JAVA】
- 数据库提供了四种事务隔离级别
- 【二】 Deep Learning训练过程
- SDUT2136数据结构实验之二叉树的建立与遍历
- 【科普·给学弟看的算法】队列的基础知识和基本运用(1):基本队列与优先队列
- 《代码大全》读书笔记及随想(day02)
- 常用算法思想之:分治思想
- Eclipse生成项目javadoc说明文档
- UICollectionViewCell 3种创建方法