★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有效
}

监听 PreDraw 阶段

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

注意选项

  • 设备配置改变时,具有ID属性的View可以保存运行状态。
  • 注意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);

// 设置预测的最小字母数
textView.setThreshold(3);
textView.setAdapter(adapter);

ImageView

简单示例

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

设置图片

  • 使用setImageDrawable()setImageBitmap()来设置图片。

Button

简单示例

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

相关工具

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");
});

RadioGroupRadioButton

<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);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容