Android开发基础:布局,监听以及一些基础知识
布局
android:id="@+id/textone" //设置id
LinearLayout:线性布局
android:layout_width="match_parent" | |
android:layout_height="match_parent" |
android:layout_width/android:layout_height可选参数
match_parent:宽度和高度匹配上一级,最外层的上一级是屏幕
wrap_content:匹配下一级,下一级多大,他多大
fill_parent:已废弃
固定数值:单位通常使用dp dp可以根据像素密度进行匹配
android:background="#485454":设置背景颜色
android:orientation="" :设置线性布局的方向
可选值:
horizontal:水平的
vertical:垂直的
android:padding="":内边距
android:layout_margin="":外边距
android:layout_gravity="":重力方向
可选值:
top。。。。。。。。。等等
RelativeLayout:相对布局
<TextView | |
android:background="#67002B" | |
android:id="@+id/textone" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/app_name" | |
/> | |
<!-- 可以做到相对位置 --> | |
<TextView | |
android:layout_toRightOf="@+id/textone" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:gravity="center" | |
android:text="@string/text_value" | |
/> |
android:layout_centerInParent=“true”:设置为父级的中心
setContentView(R.layout.activity_main); //设置布局文件
基础知识
gravity:
android:gravity="center" //设置组件内部的重力方向 | |
android:layout_gravity="center" //设置组件在父组件的位置 |
weight:
用于按照权重的比值设置宽和高的比例,只能应用于线性布局
给那个属性设置权重,就给这个属性设置0dp
android:layout_weight="1"
RadioGroup
单选框
xml中设置放方式
<RadioGroup | |
android:id="@+id/radiogroup" | |
android:layout_width="match_parent" | |
android:layout_height="100dp" | |
android:orientation="horizontal"> | |
<RadioButton | |
android:id="@+id/radio2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:checked="true" | |
android:text="male"></RadioButton> | |
<RadioButton | |
android:id="@+id/radio1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="feamle"></RadioButton> | |
</RadioGroup> | |
<Button | |
android:id="@+id/buttontwo" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="点击" | |
></Button> |
RadioGroup需要设置一个id,从而对其进行监听,每一个RadioButton都需要设置id,方便获取它的值
为RadioGroup值改变时添加一个监听器
RadioGroup radgroup = (RadioGroup) findViewById(R.id.radiogroup); | |
//第一种获得单选按钮值的方法 | |
//为radioGroup设置一个监听器:setOnCheckedChanged() | |
radgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { | |
public void onCheckedChanged(RadioGroup group, int checkedId) { | |
RadioButton radbtn = (RadioButton) findViewById(checkedId); | |
Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了" + radbtn.getText(), Toast.LENGTH_LONG).show(); | |
} | |
}); |
为button设置一个监听,来获取RadioGroup的选择值
Button viewById = findViewById(R.id.buttontwo); | |
viewById.setOnClickListener(new View.OnClickListener() { | |
RadioGroup radgroup = (RadioGroup) findViewById(R.id.radiogroup); | |
public void onClick(View v) { | |
RadioButton viewById1 = findViewById(radgroup.getCheckedRadioButtonId()); | |
Toast.makeText(getApplicationContext(), "点击事件" + viewById1.getText(), Toast.LENGTH_LONG).show(); | |
} | |
}); |
Swtich
一个开关
xml
<Switch | |
android:id="@+id/swh_status" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textOff="" | |
android:textOn="" | |
/> |
监听响应
Switch viewById1 = findViewById(R.id.swh_status); | |
viewById1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
if(buttonView.isChecked()) | |
Toast.makeText(getApplicationContext(),"on",Toast.LENGTH_SHORT).show(); | |
else | |
Toast.makeText(getApplicationContext(),"off",Toast.LENGTH_SHORT).show(); | |
} | |
}); |
ToggleButton
一个按钮,点击过程中会自动变换值
xml
<ToggleButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textOff="关闭" | |
android:textOn="打开" | |
android:id="@+id/toggle" | |
></ToggleButton> |
java
ToggleButton viewById = findViewById(R.id.toggle); | |
viewById.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
Toast.makeText(getApplicationContext(),buttonView.getText(),Toast.LENGTH_SHORT).show(); | |
} | |
}); |
SeekBar(进度条)
xml
<SeekBar | |
android:id="@+id/sb_normal" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"></SeekBar> | |
<!--该TextView仅作展示信息--> | |
<TextView | |
android:id="@+id/txt_cur" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content"></TextView> |
java
public class MainActivity extends AppCompatActivity{ | |
private static Integer times=0; | |
private SeekBar sb_normal; | |
private TextView txt_cur; | |
private Context mContext; | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.maintwo); | |
ToggleButton viewById = findViewById(R.id.toggle); | |
Switch viewById1 = findViewById(R.id.swh_status); | |
viewById.setOnCheckedChangeListener(this); | |
viewById1.setOnCheckedChangeListener(this); | |
ImageView img_pgbar = (ImageView) findViewById(R.id.img_pgbar); | |
final AnimationDrawable ad = (AnimationDrawable) img_pgbar.getDrawable(); | |
img_pgbar.postDelayed(new Runnable() { | |
public void run() { | |
ad.start(); | |
} | |
}, 1000); | |
mContext = MainActivity.this; | |
bindViews(); | |
} | |
private void bindViews() { | |
sb_normal = (SeekBar) findViewById(R.id.sb_normal); | |
txt_cur = (TextView) findViewById(R.id.txt_cur); | |
sb_normal.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { | |
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { | |
txt_cur.setText("当前进度值:" + progress + " / 100 "); | |
} | |
public void onStartTrackingTouch(SeekBar seekBar) { | |
Toast.makeText(mContext, "触碰SeekBar", Toast.LENGTH_SHORT).show(); | |
} | |
public void onStopTrackingTouch(SeekBar seekBar) { | |
Toast.makeText(mContext, "放开SeekBar", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} |
adpter
作用就是做一个listview与视图之间的适配器
list_model(作用就是自定义一个listview中的一个模板)
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<!-- 定义一个用于显示头像的ImageView --> | |
<ImageView | |
android:id="@+id/imgtou" | |
android:layout_width="64dp" | |
android:layout_height="64dp" | |
android:baselineAlignBottom="true" | |
android:paddingLeft="8dp" /> | |
<!-- 定义一个竖直方向的LinearLayout,把QQ呢称与说说的文本框设置出来 --> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:paddingLeft="8dp" | |
android:textColor="#1D1D1C" | |
android:textSize="20sp" /> | |
<TextView | |
android:id="@+id/says" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:paddingLeft="8px" | |
android:textColor="#B4B4B9" | |
android:textSize="14sp" /> | |
</LinearLayout> | |
</LinearLayout> |
activity_main.xml
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<ListView | |
android:id="@+id/list_test" | |
android:layout_height="match_parent" | |
android:layout_width="match_parent" | |
android:entries="@array/myarray"/> | |
</LinearLayout> |
MainActivity
package com.example.daytwo2021_6_8_2; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.LinearLayout; | |
import android.widget.ListView; | |
import android.widget.SimpleAdapter; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class MainActivity extends AppCompatActivity { | |
private LinearLayout p1, p2, p3, p4, p5, p6, p7, p8; | |
private String[] names = new String[]{"B神", "基神", "曹神"}; | |
private String[] says = new String[]{"无形被黑,最为致命", "大神好厉害~", "我将带头日狗~"}; | |
private int[] imgIds = new int[]{R.drawable.phone, R.drawable.ban, R.drawable.pc}; | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.maintwo); | |
initview4(); | |
} | |
private void initview4() { | |
List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>(); | |
for (int i = 0; i < names.length; i++) { | |
Map<String, Object> showitem = new HashMap<String, Object>(); | |
showitem.put("touxiang", imgIds[i]); | |
showitem.put("name", names[i]); | |
showitem.put("says", says[i]); | |
listitem.add(showitem); | |
} | |
//创建一个simpleAdapter | |
//参数说明:主类,listView中对象的list,自定义的模板,list中map值,对应的id组件 | |
SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(), listitem, R.layout.list_model, new String[]{"touxiang", "name", "says"}, new int[]{R.id.imgtou, R.id.name, R.id.says}); | |
ListView listView = (ListView) findViewById(R.id.list_test); | |
listView.setAdapter(myAdapter); | |
} | |
} |
监听
给控件设置一个监听器,监听器会一直监听某个事件的响应
当监听到事件变化就会调用回调函数
以点击事件为例学习监听器
1.给需要监听的控件设置一个id
2.java代码中设置监听
3.根据id找到监听的id(注意,这里寻找的id必须是你设置的布局里面的,不可以调用其他布局的id)
TextView viewById = findViewById(R.id.text1);
4.设置点击监听事件
viewById.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
times++; | |
if(times>5){ | |
Toast.makeText(getApplicationContext(),"还点",Toast.LENGTH_SHORT).show(); | |
} | |
else | |
Toast.makeText(getApplicationContext(),"继续点",Toast.LENGTH_SHORT).show(); | |
} | |
}); viewById.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
times++; | |
if(times>5){ | |
Toast.makeText(getApplicationContext(),"还点",Toast.LENGTH_SHORT).show(); | |
} | |
// 传入参数:外部类名称,显示文字,显示时长 | |
Toast.makeText(getApplicationContext(),"继续点",Toast.LENGTH_SHORT).show(); | |
} | |
}); |
这边的onclick就是回调方法,即当监听到响应时被调用
有多个需要监听对象时,可以采用Activity继承处理监听的类,并且实现其中回调方法,根据响应对象的id来进行不同的操作
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{ | |
private static Integer times=0; | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.maintwo); | |
ToggleButton viewById = findViewById(R.id.toggle); | |
Switch viewById1 = findViewById(R.id.swh_status); | |
viewById.setOnCheckedChangeListener(this); | |
viewById1.setOnCheckedChangeListener(this); | |
} | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
switch (buttonView.getId()){ | |
case R.id.toggle: | |
Toast.makeText(getApplicationContext(),"按钮",Toast.LENGTH_SHORT).show(); | |
case R.id.swh_status: | |
Toast.makeText(getApplicationContext(),"开关",Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} |