1. 首页
  2. IT资讯

demo简说几种控件一

Android中控件很多,下面简单的通过demo讲解几种常见的控件。这些控件都在android.widget.*这个包中,由于本人水平有限,不能够总结的很全面,还请大家见谅,如果文章中出现错误,请大家指正,本人不甚感激!

1.ImageView,ImageButton
关于这两个控件的详细说明,请参考API文档http://www.android-doc.com/reference/android/widget/package-summary.html,由于墙太高,普通的一些翻墙代理软件不能进入android官方提供的API文档网页,这里提供的是国内的在线API查阅网址,关于翻墙方法这里不多说~_~。
下面给出布局文件activity_main.xml中的代码:

点击(此处)折叠或打开

  1. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:paddingBottom=“@dimen/activity_vertical_margin”
  6.     android:paddingLeft=“@dimen/activity_horizontal_margin”
  7.     android:paddingRight=“@dimen/activity_horizontal_margin”
  8.     android:paddingTop=“@dimen/activity_vertical_margin”
  9.     android:orientation=“vertical”
  10.     tools:context=“com.example.activitytest.MainActivity” >
  11.   <TextView
  12.                android:layout_width=“match_parent”
  13.                android:layout_height=“wrap_content”
  14.                android:text=“@string/textt” />
  15.            <ImageView
  16.                android:id=“@+id/imge1”
  17.            android:scaleType=“center”  //保持原图的大小,在ImageView的中心
  18.                android:layout_width=“wrap_content”
  19.                android:layout_height=“wrap_content” />
  20.            <ImageView
  21.                android:id=“@+id/imge2”
  22.                android:scaleType=” fitStart”
  23.                android:layout_width=“100dip  //google建议像素用dip
  24.                android:layout_height=“100dip”
  25.                android:contentDescription=“@string/sf”
  26.                android:src=“@drawable/png2” />   //图片可以在这里设置,也可以通过代码实现
  27.            
  28.            <TextView
  29.                android:layout_width=“wrap_content”
  30.                android:layout_height=“wrap_content”
  31.                android:text=“@string/imagebut” />
  32.            <ImageButton
  33.                android:id=“@+id/image1”
  34.                android:contentDescription=“@string/touxiang”
  35.                android:layout_height=“wrap_content”
  36.                android:layout_width=“wrap_content” />
  37.            <ImageButton
  38.                android:id=“@+id/image2”
  39.                android:src=“@drawable/png2”
  40.                android:contentDescription=“@string/sf”
  41.                android:layout_height=“wrap_content”
  42.                android:layout_width=“wrap_content” />
  43.           
  44. </LinearLayout>

MainActivity中的代码:

点击(此处)折叠或打开

  1. public class MainActivity extends Activity {
  2.     private ImageView imag1;
  3.     private ImageView imag2;
  4.     private ImageButton imagbut1;
  5.     private ImageButton imagbut2;
  6.     @SuppressWarnings(“unused”)
  7.     private List<CharSequence> datalaguage;
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.activity_main);
  12.         imag1 = (ImageView)findViewById(R.id.imge1); //获取图片资源的id
  13.         imag2 = (ImageView)findViewById(R.id.imge2);
  14.         imagbut1 = (ImageButton)findViewById(R.id.image1);
  15.         imagbut2 = (ImageButton)findViewById(R.id.image2);
  16.         imag1.setImageResource(R.drawable.png1);//设置图片资源
  17.         
  18.         imag1.setContentDescription(getString(R.string.touxiang));//设置ContentDescription
  19.         imagbut1.setImageResource(R.drawable.png1);
  20.      imagbut1.setOnClickListener(new OnClickListener() {
  21.             
  22.             @Override
  23.             public void onClick(View v) {
  24.                 // TODO 自动生成的方法存根
  25.                 finish();
  26.             }
  27.         });
  28.     }
  29. }

运行结果:
demo简说几种控件一

由上面的代码可知,点击第三张图片时,退出当前Activity。上面只是简单的一个demo,没有更过的功能实现。更多的功能读者可以根据文档提供的属性设置。

2.TextView,EditText,Button:
下面还是通过一个简单的demo来演示:
activity_main.xml中的代码:

点击(此处)折叠或打开

  1. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:paddingBottom=“@dimen/activity_vertical_margin”
  6.     android:paddingLeft=“@dimen/activity_horizontal_margin”
  7.     android:paddingRight=“@dimen/activity_horizontal_margin”
  8.     android:paddingTop=“@dimen/activity_vertical_margin”
  9.     android:orientation=“vertical”
  10.     tools:context=“com.example.activitytest.MainActivity” >
  11.      <TextView
  12.         android:id=“@+id/mytext”
  13.         android:layout_width=“match_parent”
  14.         android:layout_height=“wrap_content”
  15.         android:gravity=“center”
  16.         android:layout_gravity=“center” />
  17.    
  18.       <EditText
  19.        android:id=“@+id/myedit1”
  20.        android:layout_width=“match_parent”
  21.        android:layout_height=“wrap_content”
  22.        android:hint=“@string/hint1” />
  23.   
  24.       <EditText
  25.        android:id=“@+id/myedit2”
  26.        android:layout_width=“match_parent”
  27.        android:layout_height=“wrap_content”
  28.        android:hint=“@string/hint2” />
  29.         <Button
  30.             android:id=“@+id/mybut”
  31.             android:layout_width=“match_parent”
  32.             android:layout_height=“wrap_content” />
  33.                   
  34. </LinearLayout>

上面简单的实现一个两个数相加的demo
MainActivity中的代码:

点击(此处)折叠或打开

  1. public class MainActivity extends Activity {
  2.     private EditText edit1,edit2;
  3.     private TextView text;
  4.     private Button but;
  5.     @SuppressWarnings(“unused”)
  6.     private List<CharSequence> datalaguage;
  7.     @Override
  8.     protected void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.activity_main);
  11.         text = (TextView)findViewById(R.id.mytext);
  12.         but = (Button)findViewById(R.id.mybut);
  13.      edit1 = (EditText)findViewById(R.id.myedit1);
  14.      edit2 = (EditText)findViewById(R.id.myedit2);
  15.      but.setOnClickListener(new OnClickListener() {
  16.             @Override
  17.             public void onClick(View v) {
  18.                 // TODO 自动生成的方法存根
  19.                 int ss1=Integer.parseInt(edit1.getText().toString());//转换成int类型
  20.                 int ss2=Integer.parseInt(edit2.getText().toString());//转换成int类型
  21.                 text.setText(String.valueOf(ss1+ss2));text.setText()接受的是String类型的参数,所以把相加后的int类型转换成String类型
  22.             }
  23.         });
  24.     }
  25. }

运行结果:
demo简说几种控件一
上面代码也比较简单,这里不过多介绍。

3.RadioGroup(单选按钮),CheckBox多选(复选)框,Spinner(下拉列表框)

点击(此处)折叠或打开

  1. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:paddingBottom=“@dimen/activity_vertical_margin”
  6.     android:paddingLeft=“@dimen/activity_horizontal_margin”
  7.     android:paddingRight=“@dimen/activity_horizontal_margin”
  8.     android:paddingTop=“@dimen/activity_vertical_margin”
  9.     android:orientation=“vertical”
  10.     tools:context=“com.example.activitytest.MainActivity” >
  11. <TextView
  12.             android:layout_width=“match_parent”
  13.             android:layout_height=“wrap_content”
  14.             android:text=“@string/text1”/>
  15.         
  16.          <RadioGroup
  17.              android:id=“@+id/myradio”
  18.              android:layout_width=“match_parent”
  19.              android:layout_height=“wrap_content”
  20.              android:orientation=“vertical”
  21.              android:checkedButton=“@+id/button”>
  22.              <RadioButton
  23.                  android:id=“@+id/button1”
  24.                  android:text=“@string/nan” />
  25.              <RadioButton
  26.                  android:id=“@+id/button2”
  27.                  android:text=“@string/nv” />
  28.          </RadioGroup>
  29.          
  30.          <TextView
  31.              android:layout_width=“match_parent”
  32.              android:layout_height=“wrap_content”
  33.              android:text=“@string/text2” />
  34.          
  35.          <RadioGroup
  36.              android:id=“@+id/myradio2”
  37.              android:layout_width=“match_parent”
  38.              android:layout_height=“wrap_content”
  39.              android:orientation=“horizontal”
  40.              android:checkedButton=“@+id/button2” >
  41.              
  42.              <RadioButton
  43.                  android:id=“@+id/button3”
  44.                  android:text=“@string/beijing”/>
  45.              <RadioButton
  46.                  android:id=“@+id/button4”
  47.                  android:text=“@string/shanghai” />
  48.              
  49.          </RadioGroup>
  50.      
  51.        <TextView
  52.             android:id=“@+id/boxtext”
  53.             android:layout_width=“match_parent”
  54.             android:layout_height=“wrap_content”
  55.             android:text=“@string/boxstr” />
  56.            
  57.          <CheckBox
  58.              android:id=“@+id/box1”
  59.              android:layout_width=“match_parent”
  60.              android:layout_height=“wrap_content”
  61.              android:text=“@string/km”
  62.              android:checked=“true” />
  63.          
  64.           <CheckBox
  65.              android:id=“@+id/box2”
  66.              android:layout_width=“match_parent”
  67.              android:layout_height=“wrap_content”
  68.              android:text=“@string/hz” />
  69.           
  70.            <CheckBox
  71.              android:id=“@+id/box3”
  72.              android:layout_width=“match_parent”
  73.              android:layout_height=“wrap_content”
  74.              android:text=“@string/xm” />
  75.          
  76.            <TextView
  77.                android:layout_width=“match_parent”
  78.                android:layout_height=“wrap_content”
  79.                android:text=“@string/booklist” />
  80.            <Spinner
  81.                android:id=“@+id/spinner1”
  82.                android:prompt=“@string/book”
  83.                android:layout_height=“wrap_content”
  84.                android:layout_width=“match_parent”
  85.                android:entries=“@array/book” /> //引用资源条目时,需用array/xxx,具体看下面设置的array资源文件
  86.           
  87.            <TextView
  88.                android:layout_width=“match_parent”
  89.                android:layout_height=“wrap_content”
  90.                android:text=“@string/citylist” />
  91.             <Spinner
  92.                 android:id=“@+id/spinner2”
  93.                 android:layout_width=“match_parent”
  94.                 android:layout_height=“wrap_content”
  95.                 android:prompt=“@string/citylist”/>
  96.       
  97.            <TextView
  98.                android:layout_width=“match_parent”
  99.                android:layout_height=“wrap_content”
  100.                android:text=“@string/languagelist” />
  101.            <Spinner
  102.                android:id=“@+id/spinner3”
  103.                android:layout_width=“match_parent”
  104.                android:layout_height=“wrap_content”
  105.                android:prompt=“@string/languagelist” />
  106. </LinearLayout>

RadioGroup,CheckBox相对简单,这里不作叙述。下面通过在布局文件中和具体代码来实现Spinner:
MainActivity中的代码:

点击(此处)折叠或打开

  1. public class MainActivity extends Activity {
  2.     private Spinner spcity;
  3.     private Spinner splanguage;
  4.     private ArrayAdapter<CharSequence> adaptercity;
  5.     private ArrayAdapter<CharSequence> adapterlaguage;
  6.     @SuppressWarnings(“unused”)
  7.     @Override
  8.     protected void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.activity_main);
  11.      spcity= (Spinner)findViewById(R.id.spinner2)
  12.      spcity.setPrompt(“请选择你喜欢的城市:”);
  13.      adaptercity = ArrayAdapter.createFromResource(this,R.array.city,android.R.layout.simple_spinner_item);//代码设置下拉资源条目
  14.      adaptercity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  15.      spcity.setAdapter(adaptercity);
  16.     
  17.      splanguage = (Spinner)findViewById(R.id.spinner3);
  18.      splanguage.setPrompt(“请选择你最喜爱的语言”);
  19.      adapterlaguage = ArrayAdapter.createFromResource(this,R.array.language,android.R.layout.simple_spinner_item);
  20.      adapterlaguage.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  21.      splanguage.setAdapter(adapterlaguage);
  22.     
  23.     }
  24. }

在下拉列表框中,要在资源文件values/*中新建资源文件:
books.xml:

点击(此处)折叠或打开

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <resources>
  3.     <string-array name=“book”>
  4.         <item>红楼梦</item>
  5.         <item>水浒传</item>
  6.         <item>西游记</item>
  7.         <item>三国演义</item>
  8.     </string-array>
  9. </resources>

city.xml:

点击(此处)折叠或打开

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <resources>
  3.     <string-array name=“city”>
  4.         <item>合肥</item>
  5.         <item>芜湖</item>
  6.         <item>马鞍山</item>
  7.     </string-array>
  8. </resources>

language.xml:

点击(此处)折叠或打开

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <resources>
  3.    <string-array name=“language”>
  4.      <item>英语</item>
  5.      <item>汉语</item>
  6.      <item>法语</item>
  7.   </string-array>
  8. </resources>

运行结果:
demo简说几种控件一
由于时间仓促,本文只是简单的通过demo感性的介绍这几种控件,还有很多属性没有细说,后续会补充更过的细节。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29876893/viewspace-1872165/,如需转载,请注明出处,否则将追究法律责任。

主题测试文章,只做测试使用。发布者:℅傍ㄖ免沦陷dε鬼,转转请注明出处:http://www.cxybcw.com/191825.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code