demo簡說幾種控制元件一

Diy_os發表於2015-12-14
Android中控制元件很多,下面簡單的透過demo講解幾種常見的控制元件。這些控制元件都在android.widget.*這個包中,由於本人水平有限,不能夠總結的很全面,還請大家見諒,如果文章中出現錯誤,請大家指正,本人不甚感激!
1.ImageView,ImageButton
關於這兩個控制元件的詳細說明,請參考API文件,由於牆太高,普通的一些翻牆代理軟體不能進入android官方提供的API文件網頁,這裡提供的是國內的線上API查閱網址,關於翻牆方法這裡不多說~_~。
下面給出佈局檔案activity_main.xml中的程式碼:

點選(此處)摺疊或開啟

  1. <LinearLayout xmlns:android=""
  2.     xmlns: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. }
執行結果:


由上面的程式碼可知,點選第三張圖片時,退出當前Activity。上面只是簡單的一個demo,沒有更過的功能實現。更多的功能讀者可以根據文件提供的屬性設定。

2.TextView,EditText,Button:
下面還是透過一個簡單的demo來演示:
activity_main.xml中的程式碼:

點選(此處)摺疊或開啟

  1. <LinearLayout xmlns:android=""
  2.     xmlns: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. }

執行結果:

上面程式碼也比較簡單,這裡不過多介紹。

3.RadioGroup(單選按鈕),CheckBox多選(複選)框,Spinner(下拉選單框)

點選(此處)摺疊或開啟

  1. <LinearLayout xmlns:android=""
  2.     xmlns: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感性的介紹這幾種控制元件,還有很多屬性沒有細說,後續會補充更過的細節。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29876893/viewspace-1872165/,如需轉載,請註明出處,否則將追究法律責任。

相關文章