Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android之控件使用,android控件

Android之控件使用,android控件

編輯:關於android開發

Android之控件使用,android控件


  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"
        
        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控件使用的知識就為大家分享完畢,內容很簡單,如有疑問可以留言討論。新手學習,高手交流。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved