1. 程式人生 > 其它 >常用控制元件使用方法

常用控制元件使用方法

技術標籤:# Androidandroid移動開發

常用控制元件使用方法

TextView

//佈局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
android:layout_height="match_parent" > /*文字 match_parent:當前控制元件大小與父佈局大小一樣,也就是父佈局來決定當前控制元件的大小 wrap_content:當前控制元件的大小剛好能包含住裡面的內容,也就是由控制元件內容來決定當前控制元件的大小 android:gravity:對齊方式 android:textColor:顏色 */ <TextView android:id="@+id/text_view"
android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#00ff00" android:textSize="24sp" android:text="This is TextView" /> </LinearLayout>

Button

//佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
/*按鈕
   android:textAllCaps="false"禁止字母進行大寫轉換
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
</LinearLayout>
//新增事件方式一
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
    	button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //在此編寫邏輯
            }
        });
    }
}
//新增方式二
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText editText;
    private ImageView imageView;
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        editText = (EditText) findViewById(R.id.edit_text);
        imageView = (ImageView) findViewById(R.id.image_view);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);

        button.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此新增邏輯
                break;
            default:
                break;
        }
    }
}

EditText

//佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
 /*按鈕
   android:textAllCaps="false"禁止字母進行大寫轉換
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
/*輸入框
 android:hint="Type somrthing here"  提示
 android:maxLines="2"   顯示的行數
*/
    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Type somrthing here"
        android:maxLines="2"
        />
</LinearLayout>
//活動  (獲取文字框的內容)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        editText = (EditText) findViewById(R.id.edit_text);
        button.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此新增邏輯
                //getText()  獲取文字框內容   toString()  轉為字串
                String inputText = editText.getText().toString();
                //用Toast彈出提示
                Toast.makeText(MainActivity.this,inputText,Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }
}

ImageView

//佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
   /*按鈕
   android:textAllCaps="false"禁止字母進行大寫轉換
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
//圖片
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img_1"
        />
</LinearLayout>
//活動 (將圖片1切換為圖片2)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        imageView = (ImageView) findViewById(R.id.image_view);
        button.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此新增邏輯
                //用setImageResource()將圖片1改成圖片2
                imageView.setImageResource(R.drawable.img_2);
                break;
            default:
                break;
        }
    }
}

ProgressBar

//佈局
//圓形進度條
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
    /*按鈕
   android:textAllCaps="false"禁止字母進行大寫轉換
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
/*進度條
 android:visibility="visible"   控制元件可見
 android:visibility="invisible" 控制元件不可見,變成透明狀態,依然佔據原來的位置
 android:visibility="gone"      控制元件不可見,不再佔用任何螢幕空間
*/
    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
//活動  (將圓環進度條隱藏或者顯示)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);

        button.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此新增邏輯
                //getVisibility()  判斷當前進度條是否可見
                if (progressBar.getVisibility() == View.GONE){
                    //設定當前進度條是否可見
                    progressBar.setVisibility(View.VISIBLE);
                }else{
                    progressBar.setVisibility(View.GONE);
                }
                break;
            default:
                break;
        }
    }
}


//水平進度條
//佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
/*按鈕
   android:textAllCaps="false"禁止字母進行大寫轉換
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
/*進度條
 android:visibility="visible"   控制元件可見
 android:visibility="invisible" 控制元件不可見,變成透明狀態,依然佔據原來的位置
 andr*/
 <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"/>
</LinearLayout>
//活動  (改變進度條的進度)

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        button.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此新增邏輯
                //獲取當前進度條的進度
                int progress = progressBar.getProgress();
                //在當前進度上加10
                progress = progress+10;
                //設定進度條的新進度
                progressBar.setProgress(progress);
                break;
            default:
                break;
        }
    }
}

AlertDialog

//佈局  對話方塊
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
/*按鈕
   android:textAllCaps="false"禁止字母進行大寫轉換
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
</LinearLayout>
//活動 (彈出對話方塊)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此新增邏輯
                //通過 AlertDialog.Builder建立一個AlertDialog例項
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                //設定對話方塊標題
                dialog.setTitle("This is Dialog");
                //設定對話方塊內容
                dialog.setMessage("Something important.");
                //設定對話方塊可否取消
                dialog.setCancelable(false);
                //呼叫setPositiveButton()方法為對話方塊設定確定按鈕的點選事件
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
					//確定事件
                    }
                });
                dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
					//取消事件
                    }
                });
                //最後呼叫show()讓對話方塊顯示
                dialog.show();
                break;
            default:
                break;
        }
    }
}

ProgressDialog

//佈局  帶有進度條的對話方塊
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
/*按鈕
   android:textAllCaps="false"禁止字母進行大寫轉換
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
</LinearLayout>
//活動  (彈出帶有進度條的對話方塊)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此新增邏輯
                //構建一個ProgressDialog物件
                ProgressDialog proressDialog = new ProgressDialog(MainActivity.this);
                //設定標題
                proressDialog.setTitle("This is ProgressDialog");
                //設定內容
                proressDialog.setMessage("Loading.......");
                //設定可否取消
                proressDialog.setCancelable(true);
                //最後呼叫show()顯示ProgressDialog
                proressDialog.show()
            default:
                break;
        }
    }
}