TextView
- 文字大小凡涩、顏色
- 顯示不下使用...
- 文字加+icon
- 下劃線棒搜、刪除線
- 跑馬燈
效果圖
代碼
activity_text_view.xml 文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_test1"
android:textColor="#000000"
android:textSize="24sp"
/>
<TextView
android:id="@+id/tv_2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/tv_test1"
android:textColor="#000000"
android:textSize="24sp"
android:maxLines="1"
android:ellipsize="end"
android:layout_marginTop="10dp"
/>
<TextView
android:id="@+id/tv_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篩選"
android:textColor="#000000"
android:textSize="24sp"
android:layout_marginTop="10dp"
android:drawableRight="@drawable/arrowdown"
android:drawablePadding="8dp"
/>
<TextView
android:id="@+id/tv_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_test1"
android:textColor="#000000"
android:textSize="24sp"
/>
<TextView
android:id="@+id/tv_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_test1"
android:textColor="#000000"
android:textSize="24sp"
/>
<TextView
android:id="@+id/tv_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="24sp"
/>
<TextView
android:id="@+id/tv_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="asdfsdf asdlfjlasjdf asdlfasld asdfj sdlfjlsajd fasdfj asdfsdf f"
android:textSize="24sp"
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
/>
</LinearLayout>
TextViewActivity文件
private TextView mTv4,mTv5,mTv6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
mTv4 = findViewById(R.id.tv_4);
mTv4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);// 中劃線
mTv4.getPaint().setAntiAlias(true); // 去除鋸齒
mTv5 = findViewById(R.id.tv_5);
mTv5.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);// 中劃線
mTv5.getPaint().setAntiAlias(true); // 去除鋸齒
mTv6 = findViewById(R.id.tv_6);
mTv6.setText(Html.fromHtml("<u>天哥在奔跑</u>"));
}
Button
- 文字大小顏色
- 背景形狀
- 按壓效果
- 點(diǎn)擊事件
效果圖
代碼
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕1"
android:textSize="20sp"
android:textColor="#F44336"
android:background="#CDDC39"
/>
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕2"
android:textSize="20sp"
android:textColor="#F44336"
android:background="@drawable/bg_btn2"
android:layout_below="@id/btn_1"
android:layout_marginTop="20dp"
/>
<Button
android:id="@+id/btn_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕3"
android:textSize="20sp"
android:textColor="#F44336"
android:background="@drawable/bg_btn3"
android:layout_below="@id/btn_2"
android:layout_marginTop="20dp"
/>
<Button
android:id="@+id/btn_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕4"
android:textSize="20sp"
android:textColor="#F44336"
android:background="@drawable/bg_btn4"
android:layout_below="@id/btn_3"
android:layout_marginTop="20dp"
android:onClick="showToast"
/>
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/btn_4"
android:layout_marginTop="20dp"
android:textColor="#000"
android:text="文字1"
android:textSize="22sp"
android:background="#FFA"
android:gravity="center"
/>
</RelativeLayout>
背景效果
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#FFC107"/>
<corners
android:radius="10dp"/>
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#FFC107"
/>
<corners
android:radius="10dp"/>
</shape>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#9C640B"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item android:state_pressed="false">
<shape>
<solid android:color="#FF9900"/>
<corners android:radius="5dp"/>
</shape>
</item>
</selector>
ButtonActivity
public class ButtonActivity extends AppCompatActivity {
private Button btn3;
private TextView tV1;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
btn3 = findViewById(R.id.btn_3);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ButtonActivity.this,"btn3被點(diǎn)擊了",Toast.LENGTH_SHORT).show();
}
});
tV1 = findViewById(R.id.tv_1);
tV1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ButtonActivity.this,"view1被點(diǎn)擊了",Toast.LENGTH_SHORT).show();
}
});
}
public void showToast(View view) {
Toast.makeText(this,"btn4被點(diǎn)擊了",Toast.LENGTH_SHORT).show();
}
}
EditText
- 常用屬性
- 監(jiān)聽事件
- 制作登錄頁面
效果圖
代碼
activity_edit_text.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/et_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16dp"
android:textColor="#FFC107"
android:hint="用戶名"
android:background="@drawable/bg_username"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:drawableLeft="@drawable/user"
android:drawablePadding="5dp"
android:maxLines="1"
/>
<!--android:inputType="number"-->
<EditText
android:id="@+id/et_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16dp"
android:textColor="#FFC107"
android:hint="密碼"
android:layout_below="@id/et_1"
android:inputType="textPassword"
android:background="@drawable/bg_username"
android:layout_marginTop="20dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:drawableLeft="@drawable/password"
android:drawablePadding="5dp"
android:maxLines="1"
/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/et_2"
android:layout_marginTop="40dp"
android:background="@drawable/bg_btn4"
android:text="登錄"
android:textColor="#fff"
android:textSize="20sp"
/>
</RelativeLayout>
EditTextActivity代碼
public class EditTextActivity extends AppCompatActivity {
private Button mLoginBtn;
private EditText mEtUsername, mEtPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text);
mLoginBtn = findViewById(R.id.btn_login);
mLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(EditTextActivity.this, "登錄成功", Toast.LENGTH_SHORT).show();
}
});
mEtUsername = findViewById(R.id.et_1);
mEtUsername.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) {
Log.d("eidtText",s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}