public void viewText(View v){
//getApplicationContext() 应用程序上下文,作用域为整个程序 全局变量
//this:当前对象(当前界面的上下文)
//参数(上下文,要显示的文本内容,显示的时间(Toast.LENGTH_LONG、Toast.LENGTH_SHORT))
Toast.makeText(getApplicationContext(),"今天天气真好!晴空万里",Toast.LENGTH_LONG).show();
}
/*
* Toast显示文本
* @prama v
* */
public void viewImage(View v){
Toast t=new Toast(this);
//用于显示图片的组件
ImageView imageView=new ImageView(this);
//为图片组件设置图片(不能以数字命名)
imageView.setImageResource(R.drawable.cat);
t.setView(imageView);
t.setDuration(Toast.LENGTH_LONG);
//设置显示的位置
t.setGravity(Gravity.TOP,0,0);
t.show();
}
/*
* 显示图文
* @param v
* */
public void viewImageText(View v){
Toast t=new Toast(this);
TextView textView=new TextView(this);
textView.setText("这是eclipse!");
ImageView imageView=new ImageView(this);
imageView.setImageResource(R.drawable.cat);
LinearLayout layout=new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(imageView);
layout.addView(textView);
t.setView(layout);
t.setGravity(Gravity.CENTER,0,0);
t.setDuration(Toast.LENGTH_LONG);
t.show();
}
TextView
private TextView textView_email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//绑定布局文件
setContentView(R.layout.activity_main);
//根据id查找组件
textView_email=findViewById(R.id.textView_email);
textView_email.setText("[email protected]");
}
private EditText editText_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//绑定布局文件
setContentView(R.layout.activity_main);
//根据id查找组件
textView_email=findViewById(R.id.textView_email);
textView_email.setText("[email protected]");
editText_name=(EditText) findViewById(R.id.editText_name);
//监听输入变化
editText_name.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
System.out.println("beforeTextChanged"+s);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println("onTextChanged"+s);
}
@Override
public void afterTextChanged(Editable s) {
System.out.println("afterTextChanged"+s);
}
});
//回车确认键事件
// editText_name.setOnEditorActionListener(new TextView.OnEditorActionListener() {
// @Override
// public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// Toast.makeText(MainActivity.this.v.getText().toString().Toast.LENGTH_LONG).show();
// return true;
// }
// });
}
Button点击事件设置的方法
实现按钮单击事件的不同方法
/*
* 1.实现View.OnClickListener下的接口
* 2.设置事件
* */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button b1,b2,b3;
private Button b4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//绑定布局文件
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button20);
b2=(Button) findViewById(R.id.button22);
b3=(Button) findViewById(R.id.button23);
b4=(Button) findViewById(R.id.button24);
//注册事件
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
//使用内部类的方式来注册事件
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"使用内部类的方式来注册事件",Toast.LENGTH_SHORT).show();
}
});
}
public void click1(View v){
Toast.makeText(this,"onClick----",Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button20:
Toast.makeText(this,"button--20",Toast.LENGTH_LONG).show();
break;
case R.id.button22:
Toast.makeText(this,"button--22",Toast.LENGTH_LONG).show();
break;
case R.id.button23:
Toast.makeText(this,"button--23",Toast.LENGTH_LONG).show();
break;
}
}
按钮透明化属性
style="?android:attr/borderlessButtonStyle"
android:background="@color/design_default_color_primary_dark"
selector选择器
新建一个activitie
在drawable下新建drawable resource file
点击ok
在这里插入代码片
显示任意的图像,如一个图标。可以加载图像从不同来源,负责计算图像的测量,这样它就可以用于任何布局管理器,并提供各种显示选项,如缩放和着色…
android:maxWidth;android:maxHeight;设置最大宽高
android:adjustViewBounds;保持宽高比
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
imageView = (ImageView) findViewById(R.id.imageView);
}
scaletype设置图片属性
android:scaleType=""
参考:https://blog.csdn.net/ren18678213431/article/details/62236932
多选框组件
设置单击事件
public class Main2Activity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private CheckBox cb_xs,cb_game,cb_movie;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
cb_xs=(CheckBox) findViewById(R.id.checkBox_xs);
cb_game=(CheckBox) findViewById(R.id.checkBox_g);
cb_movie=(CheckBox) findViewById(R.id.checkBox_m);
//注册事件
cb_xs.setOnCheckedChangeListener(this);
cb_game.setOnCheckedChangeListener(this);
cb_movie.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.checkBox_xs:
if (isChecked){
Toast.makeText(this,"你选中了小说选项",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"你取消了小说选项",Toast.LENGTH_SHORT).show();
}
break;
case R.id.checkBox_g:
if (isChecked){
Toast.makeText(this,"你选中了游戏选项",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"你取消了游戏选项",Toast.LENGTH_SHORT).show();
}
break;
case R.id.checkBox_m:
if (isChecked){
Toast.makeText(this,"你选中了电影选项",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"你取消了电影选项",Toast.LENGTH_SHORT).show();
}
break;
}
}
}
RadioButton组件
先加入一个RadioGroup,再在其中设置RadioButton
ToggleButton和Switch
设置开关单击事件
private ToggleButton toggleButton;
private Switch switch1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
toggleButton = findViewById(R.id.toggleButton);
switch1=findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(Main2Activity.this,"ischecked="+isChecked,Toast.LENGTH_SHORT).show();
}
});
}
RatingBar有大小两种样式,其中大的只适合标示,不适合交互。
numStars设置星数;stepSize改变的范围;rating初始选中的数量;isIndicator用于指示,不得改变