UI笔记

2016-03-28  本文已影响21人  yaya_pangdun

1.基础操作

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"    
    android:layout_height="match_parent"    
    android:orientation="vertical">
    <TextView android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is a textView"
        android:gravity="center"
        android:textSize="24sp"
        android:textColor="#00ff00" />
</LinearLayout>

2. 为按钮添加事件处理

public class MainActivity extends Activity implements OnClickListener {
  @Override
  public void onClick(View v) {
    switch(v.getId()) {
      case R.id.button:
      
        break;
      default:
        break;
    }
  }
}

3. 输入框

<EditText android:layout_width="match_parent"  
          android:layout_height="wrap_content"
          android:hint="Type something here"
          android:maxLines="2"
          android:id="@+id/editText"/>
.java
private EditText editText;
editText = (EditText) findViewById(R.id.editText);

String inputText = editText.getText().toString();

4. ImageView

<ImageView
  android:id="@+id/image_view"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/ic_launcher"
/>

.java
imageView,setImageResource(R.drawable.jelly_bean)

5.进度条

<ProgressBar
  ...
/>
通过设置控件的android:visibility属性来控制控件的可见性。
可选值:
- visible
- invisible
- gone
可以通过方法:
setVisibility()设置可见性,可以传入View.VISIBLE,View.INVISIBLE,View.GONE
通过方法: getVisibility()方法获取状态

可以设置成水平进度条
style="?android:attr/progressBarStyleHorizontal"
android:max = "100" #设置最大值

获取进度值
int progress = progressBar.getProgress();
progress = progress + 10;
progressBar.setProgress(progress);

6. AlertDialog

AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("This is Dialog");
dialog.setMessage("something import.");
dialog.setCancelable(false);
dialog.setPositiveButton("OK", new DialogInterface.OnClickLinstener(){
  @Override
  publlic void onClick(DialogInterface dialog, int which) {
    
  }
});

dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListenser() {
  @Override void onClick(DialogInterface dialog, int which) {

  }
});

dialog.show();

7. ProgressDialog

ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

progressDialog.setTitle("This is a Progressdialog");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();

#如果设置setCancelable(false),要通过dismiss方法来关闭对话框
上一篇下一篇

猜你喜欢

热点阅读