★15.基本視圖

View

獲取視圖寬高

int width = view.getWidth();
int height = view.getHeight();
if (width == 0 && height == 0) {
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            int width = view.getWidth();
            int height = view.getHeight();
            // width和height有效
        }
    });
} else {
    // width和height有效
}

監(jiān)聽 PreDraw 階段

mView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
        mView.getViewTreeObserver().removeOnPreDrawListener(this);
        // Todo: do something
        return true;
    }
});

注意選項

  • 設(shè)備配置改變時,具有ID屬性的View可以保存運行狀態(tài)。
  • 注意getWidth()getHeight()只有在onCreate(),onStart(),onResume()之后才有大小,否則返回0。

TextView

<TextView
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="用戶名"
        android:textColor="#000000"
        android:textSize="18sp"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:maxLines="1"
        android:ellipsize="end"/>

EditText

代碼

<EditText
        android:id="@+id/crime_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/crime_title_hint"/>

TextWatcher

textView.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) { }
});

AutoCompleteTextView

String[] strArr = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, strArr);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txt);

// 設(shè)置預(yù)測的最小字母數(shù)
textView.setThreshold(3);
textView.setAdapter(adapter);

ImageView

簡單示例

<ImageView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/icon_rank1_background"/>

設(shè)置圖片

  • 使用setImageDrawable()setImageBitmap()來設(shè)置圖片。

Button

簡單示例

<Button android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00ffff"
        android:text="文字"
        android:gravity="center"/>

相關(guān)工具

ImageButton

<ImageButton
         android:id="@+id/ranklist_cloes"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/icon_share_close"
         android:background="@null"/>

ToggleButton

<ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text"/>
ToggleButton toggleButton = (ToggleButton) v.findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(view -> {
    if (((ToggleButton) view).isChecked())
        Log.d("TAG", "isChecked");
    else
        Log.d("TAG", "notChecked");
});

CheckBox

<CheckBox
        android:id="@+id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="text"/>
CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox);
checkBox.setOnClickListener(view -> {
    if (((CheckBox) view).isChecked())
        Log.d("TAG", "isChecked");
    else
        Log.d("TAG", "notChecked");
});

RadioGroup、RadioButton

<RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton"
            android:layout_weight="1"/>

    <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton"
            android:layout_weight="1"/>
</RadioGroup>
RadioGroup view = (RadioGroup) v.findViewById(R.id.radioGroup);
view.setOnCheckedChangeListener((radioGroup, i) -> {
    RadioButton radioButton = (RadioButton) radioGroup.findViewById(R.id.radioButton1);
    if (radioButton.isChecked())
        Log.d("TAG", "isChecked");
    else
        Log.d("TAG", "notChecked");
});

ProgressBar

<ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal"/>
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
progressBar.setMax(200);
progressBar.setProgress(100);

TimePicker

xml

<TimePicker android:id="@+id/timePicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

代碼

TimePicker timePicker = (TimePicker) itemView.findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);

DatePicker

xml

<DatePicker android:id="@+id/id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:datePickerMode="calendar"/>

代碼

mDatePicker.init(year, month, day, null);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容