文章目錄
- Button組件
- TextView組件
- EditText組件
- ImageView組件
- AlertDialog組件
- ProgressBar組件
- 總結:本文介紹了很多好玩有有用的組件,主要為讀者提供一些組件的模板,并未給出完整可運行的project,具體的引數設計以及應用,還需要讀者再參考其他資料,
組件的應用大同小異,因而我們主要以展示代碼為主,以方便讀者直接復制粘貼應用~~~~
Button組件
首先在UI界面,我們要定義Button的展示效果
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 3"
/>
</LinearLayout>
其對應的效果為:

單純的定義一個UI組件,沒有任何意義,所以我們還要在activity設定他的一些屬性,譬如我們生成他的一個點擊事件,實作代碼如下:
Button button=(Button) findViewById(R.id.button_3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//輸入想要執行的命令
System.out.println("神通");
}
});
同樣,在activity中,首先要定義一個Button變數,然后找到UI中對應的ID,setOnClickListener()是為其設定點擊事件,如此便實作了點擊按鈕的功能,
TextView組件
簡簡單單
UI中,可以設定TextView的很多屬性,比如位置、顏色、大小,在這里就不一一列舉,最關鍵的屬性是ID,以便于在activity中呼叫,
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is TextView" />
activity中,我們用setText()顯示內容,
TextView textView1=(TextView)findViewById(R.id.text_view);
textView1.setText("a");
好像顯示的資料型別只能如圖?

EditText組件
該組件是一個很好用的組件,實作了真正的人機互動,允許用戶在該控制元件里面編輯內容,并可以對其進行處理,
代碼如下
<EditText
android:id="@+id/editext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="在這里輸入"
/>
activity中的使用也很簡單,我們在這里綜合上面三個組件,在EditText中隨意寫入一些內容,然后通過Button的點擊事件,將寫入EditText的內容傳遞給TextView,實作代碼如下:
Button button=(Button) findViewById(R.id.button_3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText edittext1=(EditText)findViewById(R.id.editext);
String a=edittext1.getText().toString();
TextView textView1=(TextView)findViewById(R.id.text_view);
textView1.setText(a);
}
});
it is good!!!
ImageView組件
ImageView主要用于顯示圖片,通常照片放在drawable中,我們放置兩張命名為lv.jpg和lv1.jpg,
UI界面和activity界面代碼分別如下:
我們首先初始化了一張照片lv1,
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/lv1"
/>
然后在activity中,通過代碼事先修改imageview的內容:
ImageView imageView1=(ImageView) findViewById(R.id.image_view);
imageView1.setImageResource(R.drawable.lv2);
哦吼,簡簡單單,
AlertDialog組件
該組件用于在當前的界面彈出對話框,置頂于所有界面元素之上,一般用于提示一些非常重要的內容,組件代碼如下:
AlertDialog.Builder dialog = new AlertDialog.Builder (Third_Activity.this);
dialog.setTitle("提示!");//為這個對話框設定標題
dialog.setMessage("您出錯了,要否要退出?");//提示內容
dialog.setCancelable(false);//可否用Back鍵關閉對話框等屬性
dialog.setPositiveButton("是", new DialogInterface.OnClickListener() {
//呼叫setPositiveButton() 方法為對話框設定確定按鈕的點擊事件
@Override
public void onClick(DialogInterface dialog, int which) {
//退出當前activity
android.os.Process.killProcess(android.os.Process.myPid());
}
});
dialog.setNegativeButton("否", new DialogInterface.OnClickListener() {
//呼叫setNegativeButton() 方法設定取消按鈕的點擊事件
@Override
public void onClick(DialogInterface dialog, int which) {
Intent aaa=new Intent(Third_Activity.this,SecondActivity.class);
startActivity(aaa);
}
});
dialog.show(); //將對話框顯示出來
在該段代碼里面,可以為每一次選擇設定點擊事件,進行跳轉,
ProgressBar組件
ProgressBar是在界面上顯示的進度條,表示我們的程式正在加載一些資料,直接上代碼:
UI界面,常規屬性定義:
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
/>
activity中,需要注意的是,在定義ProgressBar型別的一個變數的時候,要將其放在前面,我們此次為一個點擊事件設定了進度條,
public class Third_Activity extends AppCompatActivity {
ProgressBar progressBar ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third_);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
Button button=(Button) findViewById(R.id.button_3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int progress = progressBar.getProgress();
progress = progress + 10;
progressBar.setProgress(progress);
EditText edittext1=(EditText)findViewById(R.id.editext);
String a=edittext1.getText().toString();
TextView textView1=(TextView)findViewById(R.id.text_view);
textView1.setText(a);
ImageView imageView1=(ImageView) findViewById(R.id.image_view);
imageView1.setImageResource(R.drawable.lv2);
}
});
}
}
嘔吼,可以搓搓手去看看代碼效果了~~~~
總結:本文介紹了很多好玩有有用的組件,主要為讀者提供一些組件的模板,并未給出完整可運行的project,具體的引數設計以及應用,還需要讀者再參考其他資料,
你學的怎么樣呢?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/273265.html
標籤:其他
