AutoCompleteTextView和MultiAutoCompleteTextView
MainActivity.java
package com.example.splashacvity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
import org.apache.harmony.javax.security.sasl.SaslException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Packet;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView acTextView;
private String[] res = {"beijin3","beijing1","beijing2","shanghai1","shanghai2"};
private MultiAutoCompleteTextView mtAtCompleteTtVw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//将布局xml文件引入到Activity当中
//setContentView(R.layout.activity_main);
setContentView(R.layout.demo2);
/*第一步:初始化控件
* findViewById返回的是View对象,是所有控件的父类,因此需要进行强制类型转换
* 相当于没有进行实例化,而是从xml处得到的值进行了实例化
* 第二步,需要一个适配器,当输入文本时,适配器能够监控到,并给出匹配的条目
* 第三步:初始化数据源,匹配文本框输入的内容
* */
acTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
/**
*
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,res);
//第4步:讲Adapter和当前的AutoCOmpleteTextView绑定
acTextView.setAdapter(adapter);
//第5步:设置属性,当输入几个字符的时候进行匹配,这个要在控件上进行设置
mtAtCompleteTtVw = (MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView2);
mtAtCompleteTtVw.setAdapter(adapter);
//设置以逗号作为分隔符作为结束的符号
mtAtCompleteTtVw.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
demo2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="3"
android:hint="请输入你想要查找的关键词" />
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="3"
android:hint="请输入你想要查找的关键词" />
</LinearLayout>
Button和ImageButton
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_name" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>
监听事件的三种写法
所有的控件都会在R.java中注册,因此可以在java代码中通过R.id的方式进行寻找,找到之后就可以实例化变量了。
1、匿名内部类实现点击的监听事件
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_name" />
package com.example.splashacvity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo2);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("你点击了按钮");
}
});
}
}
2、通过独立类实现监听事件
package com.example.splashacvity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.*;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo2);
button = (Button)findViewById(R.id.button);
// button.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// System.out.println("你点击了按钮");
// }
// });
button.setOnClickListener(myClickListener);
}
public OnClickListener myClickListener = new OnClickListener() {
@Override
public void onClick(View view) {
Log.i("tag","你点击了我");
}
};
}
3、实现接口的方式来实现监听点击事件
package com.example.splashacvity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.*;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements OnClickListener{
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo2);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Log.i("tag","你点击了我(使用实现接口的方式监听点击事件)");
}
}
ToggleButton的使用(ToggleButton就相当于是开关一下,点击一下开启,再点击一下关闭,一直循环)
在drawable文件夹下定义两个图片,off.png和on.png表示关灯和开灯。
设置ImageView的背景,background属性,来很方便得改变ImageView的显示
MainActivity.java
package com.example.splashacvity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton.*;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity{
private ImageView img;
private ToggleButton tb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo2);
tb = (ToggleButton)findViewById(R.id.toggleButton);
img = (ImageView)findViewById(R.id.imageView);
tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {//这里是选中事件,不是点击事件
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
img.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off);
}
});
}
}
demo2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:textOn="开"
android:textOff="关"
android:text="ToggleButton" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/off"/>
</LinearLayout>
CheckBox复选框以及复选框的点击处理
package com.example.splashacvity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity{
private CheckBox cb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo2);
cb = (CheckBox)findViewById(R.id.basketball);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
Log.i("isChecked","" + isChecked);
Log.i("isChecked",compoundButton.getText().toString());
}else{
Log.i("isNotChecked",isChecked + "");
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<CheckBox
android:checked="false"
android:id="@+id/basketball"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="篮球" />
</LinearLayout>
2019-06-23 20:58:52.292 6019-6019/com.example.splashacvity I/isChecked: true
2019-06-23 20:58:52.292 6019-6019/com.example.splashacvity I/isChecked: 篮球
2019-06-23 20:58:53.603 6019-6019/com.example.splashacvity I/isNotChecked: false
2019-06-23 20:58:57.707 6019-6019/com.example.splashacvity I/isChecked: true
2019-06-23 20:58:57.708 6019-6019/com.example.splashacvity I/isChecked: 篮球
2019-06-23 20:58:58.787 6019-6019/com.example.splashacvity I/isNotChecked: false
2019-06-23 20:59:02.654 6019-6019/com.example.splashacvity I/isChecked: true
2019-06-23 20:59:02.654 6019-6019/com.example.splashacvity I/isChecked: 篮球
使用RadioButton,首先引入RadioGruop,多个RadioButton被包含在RadioGroup当中。
package com.example.splashacvity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity{
private RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo2);
rg = (RadioGroup)findViewById(R.id.doSomething);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.food:
Log.i("food",i + "去吃饭");
break;
case R.id.movie:
Log.i("movie",i + "去看电影");
break;
case R.id.study:
Log.i("study",i + "去学习");
break;
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:id="@+id/doSomething"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/food"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="去吃饭" />
<RadioButton
android:id="@+id/movie"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="去看电影" />
<RadioButton
android:id="@+id/study"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="去学习" />
</RadioGroup>
</LinearLayout>
I/food: 2131165245去吃饭
I/movie: 2131165266去看电影
I/study: 2131165314去学习
I/movie: 2131165266去看电影
I/food: 2131165245去吃饭
I/study: 2131165314去学习
在底部水平居中,可以使用/的方式累加
如果子控件1设置layout_weitht=2,子控件2设置layout_weight=1,并且两个子控件都设置了height=match_parent,那么布局将会和预想的相反,原先占两份的占一份,原先占两份的现在占一份。
线性布局内部可以嵌套线性布局
相对布局
控件相对于控件的布局
帧布局,布局使用laoout_gravity
效果:
绝对布局,简单了解一下
以固定的数值来固定位置,所以不灵活。当手机屏幕大小改变时,布局机会发生改变。
表格布局
shrinkColumns="3",表示第3列是可以进行收缩的
设置shrinkColumns=2时,第三个按钮就会占满屏幕
layout_column 和 layout_span
使用表格布局 实现一个计算器布局
gravity表示这个TextView内部的组件相对于这个TextView的布局
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:stretchColumns="*"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="30"
android:textSize="50dp"
android:background="#F5F5F5"
android:gravity="right|center_vertical" />
/>
<TableRow>
<Button
android:id="@+id/c7"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
/>
<Button
android:id="@+id/c8"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
/>
<Button
android:id="@+id/c9"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
/>
<Button
android:id="@+id/cDiv"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/c4"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
/>
<Button
android:id="@+id/c5"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
/>
<Button
android:id="@+id/c6"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
/>
<Button
android:id="@+id/cMul"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/c1"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
/>
<Button
android:id="@+id/c2"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
/>
<Button
android:id="@+id/c3"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
/>
<Button
android:id="@+id/cSub"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/c0"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
/>
<Button
android:id="@+id/cDot"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
/>
<Button
android:id="@+id/cEqu"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
/>
<Button
android:id="@+id/cAdd"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
/>
</TableRow>
<TableRow android:layout_marginTop="20dp">
<Button
android:id="@+id/cB"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
/>
<Button
android:id="@+id/cC"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
/>
<Button
android:id="@+id/cD"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D"
/>
<Button
android:id="@+id/cE"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E"
/>
</TableRow>
<TableRow>
<Button
android:layout_span="2"
android:id="@+id/cA"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
/>
<Button
android:layout_span="2"
android:id="@+id/cF"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F"
/>
</TableRow>
<TableRow>
<ToggleButton
android:layout_span="2"
android:id="@+id/growStep"
android:textSize="30dp"
android:checked="false"
android:textOff="十进制模式"
android:textOn="十六进制模式"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_span="2"
android:id="@+id/cCle"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AC"
/>
</TableRow>
</TableLayout>