Android常用设置

设置属性

1、声明一个public void 方法,并且必须有一个View类型的参数

2、在属性面板中设置控件的onClick属性为第一步中的方法名

接口

1、实现接口:

implements View.OnClickListener

2、设置监听器:

button.setOnClickListener(this);

3、重写onclick方法

@Override 
public void onClick(View view) 
{ 
    
}  

固定写法

 btnClick.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {                
        //里面写点击后想要实现的效果
       

 });

显示Toast消息

1、简洁写法:

Toast.makeText(getApplicationContext(), "显示内容", Toast.LENGTH_LONG).show();

2、用同一个对象:

//声明
Toast tos;
//赋值
tos=Toast.makeText(this,"",Toast.LENGTH_SHORT);
//设置显示文字
tos.setText("显示文字");
//显示
tos.show();

3、取消显示

tos.cancel();

4、设置显示时间

tos.setDuration(Toast.LENGTH_SHORT);

5、设置显示位置

//右上角显示,向下偏移50dp
tos.setGravity(Gravity.TOP|Gravity.RIGHT,0,50);

格式化输出

String.format("%.1f",3.14159);//返回字符串“3.1”
String.format("%d是质数",17);//返回字符串“17是质数”

获取系统日期和时间

Calendar c=Calendar.getInstance();
//年
c.get(Calendar.YEAR);
//月
c.get(Calendar.MONTH);
//日
c.get(Calendar.DAY_OF_MONTH);
//小时
c.get(Calendar.HOUR_OF_DAY);
//分钟
c.get(Calendar.MINUTE); 

获取资源

getResources().getString(R.string.资源名称); 

类型转换

String转Double

double f=Double.parseDouble(12.5);
//String转int
int f= Integer.parseInt("100");

显示Alert对话框

new AlertDialog.Builder(this)
    //设置内容
    .setMessage("你喜欢Android手机吗?")
    //设置标题
    .setTitle("Android问卷调查")
    //设置不允许按返回键退出对话框
    .setCancelable(false)
    //设置图标
    .setIcon(R.mipmap.ic_launcher)
    //设置积极的按钮
    .setPositiveButton("喜欢",this)
    //设置中性的按钮
    .setNeutralButton("没意见",this)
    //设置消极的按钮
    .setNegativeButton("讨厌",this)
    //显示
    .show();

注意:

要实现监听按钮的点击事件,必须实现接口:

public class MainActivity extends AppCompatActivity implements DialogInterface.OnClickListener{
    
}

并实现方法:

@Override
public void onClick(DialogInterface dialogInterface, int i) 
{    
    //判断用户点击了哪个按钮
    if(i==DialogInterface.BUTTON_NEGATIVE)
    {        
        //消极的      
    }    
    else if(i==DialogInterface.BUTTON_NEUTRAL)
    {        
        //中性的
    }    
    else    
    { 
        //积极的      
    }
}

日期Alert

new DatePickerDialog(this,this,
        c.get(Calendar.YEAR),
        c.get(Calendar.MONTH),
        c.get(Calendar.DAY_OF_MONTH)).show(); 
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    txv_rq.setText("日期:"+year+"/"+(month+1)+"/"+dayOfMonth);
}  

时间Alert

new TimePickerDialog(this,this,
    c.get(Calendar.HOUR_OF_DAY),
    c.get(Calendar.MINUTE),
    true).show(); 
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    txv_sj.setText("时间:"+hourOfDay+":"+minute);
}

颜色设置

txv=findViewById(R.id.txv);

//设置内置的红色
txv.setTextColor(Color.RED);
//按RGB设置
txv.setTextColor(Color.rgb(0,255,0));
//按ARGB设置
txv.setTextColor(Color.argb(127,0,255,0));
//按16进制设置
txv.setTextColor(Color.parseColor("#FF00FF"));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值