Android系统为我们提供了大量的控件,例如:开关控件、单选按钮、多选按钮、单选菜单等等,那么这些控件如何使用呢?本篇我将带领大家一道学习一下如何使用这些控件。所谓无图无真相,先让大家看一下效果图:
下面我们就一起学习一下这些控件的使用吧,首先是我们这边效果的布局文件:
<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}" > <LinearLayout android:id="@+id/lative" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FF000000" > <TextView android:id="@+id/Textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <ProgressBar android:id="@+id/Barfirst" style="?android:attr/progressBarStyleHorizontal" android:max="100" android:visibility="gone" android:layout_width="200dip" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/Bartwo" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/open" /> <CheckBox android:id="@+id/reading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/reading" /> <CheckBox android:id="@+id/music" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/music" /> <CheckBox android:id="@+id/youyong" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/youyong" /> <RadioGroup android:id="@+id/radio" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/nan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/nan" /> <RadioButton android:id="@+id/nv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/nv" /> </RadioGroup> <ToggleButton android:id="@+id/togbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="@string/biejingquit" android:textOff="@string/biejingopen" /> <Button android:id="@+id/first" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/xiayiye" /> </LinearLayout> </RelativeLayout>
下面是我们的主Activity,为了方便大家拷贝学习,我将各个控件的事件都添加了注释,大家一看应该就能明白,这里就不在赘述。
public class Activityone extends Activity { private Button mybutton;//打开进度条按钮 private Button mybuttonfirst;//普通跳转按钮 private ProgressBar mybarfirst;//进度条1 private ProgressBar mybartwo;//进度条2 private TextView mytext;//文本控件 private CheckBox reading;//多选按钮 private CheckBox music;//多选按钮 private CheckBox youyong;//多选按钮 private RadioButton nan;//单选按钮 private RadioButton nv;//单选按钮 private ToggleButton togbutton;//开关控件 private LinearLayout lative;//主布局控件 private int cont = 0;//记录进度条大小 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.All();//设置activity全屏显示 setContentView(R.layout.activity_one); mybutton=(Button)findViewById(R.id.button); mybuttonfirst=(Button)findViewById(R.id.first); mytext=(TextView)findViewById(R.id.Textview); mybarfirst=(ProgressBar)findViewById(R.id.Barfirst); mybartwo=(ProgressBar)findViewById(R.id.Bartwo); reading=(CheckBox)findViewById(R.id.reading); music=(CheckBox)findViewById(R.id.music); youyong=(CheckBox)findViewById(R.id.youyong); nan=(RadioButton)findViewById(R.id.nan); nv=(RadioButton)findViewById(R.id.nv); togbutton=(ToggleButton)findViewById(R.id.togbutton); lative=(LinearLayout)findViewById(R.id.lative); //开关点击事件管理 togbutton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (togbutton.isChecked()) { lative.setBackgroundColor(0xFFF3F3F3); }else{ lative.setBackgroundColor(0xFF000000); } } }); //单选按钮 nan.setOnClickListener(new myradiobuttonlistener()); nv.setOnClickListener(new myradiobuttonlistener()); //进度条开关管理 mybutton.setOnClickListener(new mybuttonlistener()); //多选按钮点击事件 reading.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (((CheckBox) v).isChecked()) { Toast.makeText(getApplicationContext(), R.string.reading, Toast.LENGTH_SHORT).show(); } } }); //多选按钮点击事件 music.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (((CheckBox) v).isChecked()) { Toast.makeText(getApplicationContext(), R.string.music, Toast.LENGTH_SHORT).show(); } } }); //多选按钮点击事件 youyong.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (((CheckBox) v).isChecked()) { Toast.makeText(getApplicationContext(), R.string.youyong, Toast.LENGTH_SHORT).show(); } } }); //普通跳转按钮点击事件管理 mybuttonfirst.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Activityone.this, Activitytwo.class); Activityone.this.startActivity(intent); } }); } //进度条控制 class mybuttonlistener implements OnClickListener{ @Override public void onClick(View v) { if(cont==0){ mybarfirst.setVisibility(View.VISIBLE); mybartwo.setVisibility(0); mytext.setText(R.string.addone); mybutton.setText(R.string.add); }else if(cont<=100){ mybarfirst.setProgress(cont); mybarfirst.setSecondaryProgress(cont+10); }else{ mybarfirst.setProgress(0); mybarfirst.setSecondaryProgress(0); mybarfirst.setVisibility(View.GONE); mybartwo.setVisibility(View.GONE); mytext.setText(R.string.hello_world); mybutton.setText(R.string.open); cont=-10; } cont+=10; } } //单选点击事件 class myradiobuttonlistener implements OnClickListener{ @Override public void onClick(View v) { RadioButton rb = (RadioButton) v;//获得被点击的控件 Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show();//rb.getText():得到被点击的选项 } } //设置应用全屏显示 private void All(){ requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } //系统菜单(menu菜单) public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 1, 1, R.string.quit); menu.add(0, 2, 2, R.string.about); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId()==1){ //创建一个提示对话框 AlertDialog.Builder builder = new AlertDialog.Builder(Activityone.this);//获得一个警戒对话框 builder.setTitle(R.string.menu_quit); builder.setMessage(R.string.menu_mager); builder.setPositiveButton(R.string.menu_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); builder.setNegativeButton(R.string.menu_no, null); builder.show(); } return super.onOptionsItemSelected(item); } }
下面为大家介绍一下弹出弹出对话框的知识,还是老一套,先看代码,首先是布局文件:
<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}" > <LinearLayout android:orientation="vertical" android:id="@+id/lineartwo" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/texttwo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/duihua" android:textSize="20dip" /> <Button android:id="@+id/buttontwo1" android:text="@string/danxuan" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/buttontwo2" android:text="@string/duoxuan" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/buttontwo3" android:text="@string/liebiao" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/buttontwo4" android:text="自定义弹出窗口" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/buttontwo5" android:text="@string/xiayiye" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout>
下面是主Activity代码:
public class Activitytwo extends Activity{ private Button mybutton1;//单选对话框 private Button mybutton2;//多选对话框 private Button mybutton3;//列表对话框 private Button mybutton4;//自定义弹出对话框 private Button mybutton5;//普通跳转按钮 private String arr[]={"男","女","少年","青年","中年","老年","已婚","未婚","本科","硕士","博士"}; private String brr[]={"游泳","听音乐","看书","打电脑游戏","书法","看电视","玩手机","唱歌","旅游","学习"}; private boolean[] brrs={true,true,false,false,true,false,false,false,false,false};//与brr[]一一对应,true:默认被选择;false:默认不被选择 private String crr[]={"北京","上海","天津","河南","湖南","湘潭","长沙","武汉","福建","澳门","香港","深圳","广州"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two2); mybutton1=(Button)findViewById(R.id.buttontwo1); mybutton2=(Button)findViewById(R.id.buttontwo2); mybutton3=(Button)findViewById(R.id.buttontwo3); mybutton5=(Button)findViewById(R.id.buttontwo5); mybutton4=(Button)findViewById(R.id.buttontwo4); //单选提示对话框 mybutton1.setOnClickListener(new mybutton1listener()); //多选提示对话框 mybutton2.setOnClickListener(new mybutton2listener()); //下拉提示对话框 mybutton3.setOnClickListener(new mybutton3listener()); //自定义弹出对话框 mybutton4.setOnClickListener(new mybutton4listener()); //普通跳转按钮 mybutton5.setOnClickListener(new mybutton5listener()); } //单选事件监听 class mybutton1listener implements OnClickListener{ @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(Activitytwo.this); builder.setTitle(R.string.duoxuan);//设置标题 builder.setIcon(R.drawable.ic_launcher);//设置图标 builder.setSingleChoiceItems(arr , 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Activitytwo.this, " "+"你选择的是:"+arr[which], Toast.LENGTH_LONG).show(); //dialog.dismiss(); } });//设置选择的内容 builder.setPositiveButton(R.string.menu_yes, null); builder.show(); } } //多选事件监听 class mybutton2listener implements OnClickListener{ @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(Activitytwo.this); builder.setTitle(R.string.duoxuan); builder.setIcon(R.drawable.ic_launcher); builder.setMultiChoiceItems(brr, brrs, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { for(int i=0;i<brr.length;i++){ if(i==which&&isChecked){ Toast.makeText(Activitytwo.this, " "+brr[i], Toast.LENGTH_SHORT).show(); } } } }); builder.setPositiveButton(R.string.menu_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } //下拉列表事件监听 class mybutton3listener implements OnClickListener{ @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(Activitytwo.this); builder.setTitle(R.string.liebiao); builder.setIcon(R.drawable.ic_launcher); builder.setItems(crr, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Activitytwo.this, " "+crr[which], Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton(R.string.menu_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } } class mybutton4listener implements OnClickListener{ @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(Activitytwo.this); builder.setTitle("登录窗口"); builder.setIcon(R.drawable.ic_launcher); View login = Activitytwo.this.getLayoutInflater().inflate(R.layout.info, null);//设置弹出窗口的视图 builder.setView(login); final AlertDialog alerrt = builder.create(); alerrt.show(); Button button1 = (Button) login.findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { alerrt.dismiss(); } }); } } //普通跳转 class mybutton5listener implements OnClickListener{ @Override public void onClick(View v) { Intent intent = new Intent(Activitytwo.this,Activitythree.class); Activitytwo.this.startActivity(intent); } } }
好了,关于Android控件使用的知识就为大家分享完毕,内容很简单,如有疑问可以留言讨论。新手学习,高手交流。