安卓Studio笔记
按钮
-
android:text=“我爱你” 设置显示的文字
2.监听器
通过匿名内部类 要设置id
通过onClick
public void MyClick(View v)
{
Toast.makeText(MainActivity.this,"单击了",Toast.LENGTH_SHORT).show();
}
需要在Antivi_main.xml添加onClick并设置名字,和方法名一致
图片按钮
- 和Button的相同之处,单击都可以触发onClick事件
- 不同点:ImageButton没有android:text属性
<ImageButton
android:id="@+id/im1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/bgstart"
android:background="#0000"
/>
//background="#0000"设置背景为透明
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dsfihoihdgoifdhi"
/>
<ImageButton
android:id="@+id/im2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/adsdfd1"
android:background="#0000"
/>
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//设置全屏显示
ImageButton bt=(ImageButton) findViewById(R.id.im1);
bt.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"单击了开始",Toast.LENGTH_LONG).show();
}
});
ImageButton bt1=(ImageButton) findViewById(R.id.im2);
bt1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"啥都没有",Toast.LENGTH_LONG).show();
}
});
单选按钮
<RadioButton
android:id="@+id/rg1"//在获取值时添加
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/>//默认选中
在一组单选按钮中只能有一个被选中,需要添加RadioGroup包裹RadioButton
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保密"/>
</RadioGroup>
在改变单选按钮的值时获取改变的值
RadioGroup rg=(RadioGroup)findViewById(R.id.rg1);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton r=(RadioButton)findViewById(checkedId);
r.getText();
Toast.makeText(MainActivity.this,"性别"+r.getText(),Toast.LENGTH_SHORT).show();
}
});
添加按钮获取改变的
该方法的 RadioGroup rg;需要定义为全局变量
<RadioGroup
android:id="@+id/rg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保密"/>
</RadioGroup>
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
/>
Button b=(Button)findViewById(R.id.b1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<rg.getChildCount();i++)
{
RadioButton r=(RadioButton)rg.getChildAt(i);
if(r.isChecked()){
Toast.makeText(MainActivity.this,"性别"+r.getText(),Toast.LENGTH_SHORT).show();
break;
}
}
}
});
判断是否选中想要的单选框
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是不是你亲爱的爸爸"
android:textSize="19dp"
android:textColor="#55E405E4"
/>
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="是"/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不是"/>
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不知道"/>
</RadioGroup>
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
/>
复选框
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美术"/>
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="语文"
android:checked="true"/>//默认为选中状态
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="英语"/>
lick1=(CheckBox) findViewById(R.id.cb1);
lick1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (lick1.isChecked()){
//判断是否选中
Toast.makeText(MainActivity.this, lick1.getText(),Toast.LENGTH_SHORT).show();
}
}
});