1.Android中的Toast是一种简易的消息提示框。
使用需引入包:
import android.widget.Toast;
Toast.makeText(this,"red",Toast.LENGTH_LONG).show();
2.关联布局
新建xml-》layout xml - > setContentView(R.layout.yflayout);
3根据id获取一个控件 添加点击事件 第一种
Button yfcBtn = findViewById(R.id.buttonyfc);
yfcBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("cvbnmsdfghj");
Toast.makeText(MainActivity.this,"hhhh",Toast.LENGTH_LONG).show();;
}
});
4设置控制台输出过滤器
edit filter config -> log tag - >System.out
5 点击事件第二种
yfcBtn.setOnClickListener(new MyclickListener());
class MyclickListener implements View.OnClickListener{
@Override
public void onClick(View view){
System.out.println("cvbnmsdfghj");
Toast.makeText(MainActivity.this,"hhkhh",Toast.LENGTH_LONG).show();;
}
}
//第三种
public class MainActivity extends AppCompatActivity implements View.OnClickListener
yfcBtn.setOnClickListener(this);
@Override
public void onClick(View v) {
System.out.println("cvbnmsdfghj");
Toast.makeText(MainActivity.this,"hhggkhh",Toast.LENGTH_LONG).show();;
//
}
第一种 和第三种使用最多
第四种
<Button
android:id="@+id/buttonyfc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClickyyy"
android:text="Button" />
public void onClickyyy(View view){
System.out.println("cvbnmsdfghj");
Toast.makeText(MainActivity.this,"hhkhh",Toast.LENGTH_LONG).show();;
}
6.布局 类型
ConstraintLayout
LinearLayout
7输入框
EditText textFiled = findViewById(R.id.editTextyfc);
8全局
private EditText textFiled;
9打电话
<uses-permission android:name="android.permission.CALL_PHONE"/>
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 1){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
callph0ne();;
}else {
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
if(text.equals("")){
Toast.makeText(MainActivity.this,"号码",Toast.LENGTH_LONG).show();
}else {
//创建意图
Intent intent = new Intent();
//创建动作
intent.setAction(Intent.ACTION_CALL);
//动作的数据
intent.setData(Uri.parse("tel://"+ text));
//开启界面
startActivity(intent);
}
10线性布局 相对布局 帧布局 表格布局绝对布局(少) 约束布局
11安卓里的textview就是ios里的UILabel
PlainText 相当于ios里的 UITextFiled
hint相当于placeHolder
MultilineText 相当于 UITextView
12 添加新类
<activity android:name=".BaseActivity"/>
BaseActivity运行时权限轮子
public class MainActivity extends BaseActivity
final String phoneNum
13先写变量 然后再自动生成声明代码 ALT+Enter
private static final String TAG = "MMMM";
Log.v(TAG,"v级别");
Log.d(TAG,"D级别");
Log.i(TAG,"i级别");
Log.w(TAG,"w级别");
Log.e(TAG,"e级别");
14局部邊全部 alt cmd f
15
TextUtils.isEmpty(qq) 文本框空
讀寫文件
try {
//寫文件
File file = new File(this.getFilesDir(),"info.txt");
FileOutputStream outS = new FileOutputStream(file);
String info = "hhh" + "#hhhj";
outS.write(info.getBytes());
outS.close();;
//讀
if(file.exists() && file.length() > 0){
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String str = br.readLine();
String qqqq = info.split("#")[0];
qqno1.setText(qqqq);
}
}catch (Exception e){
}
16获取存储空间方法不准
long datasize = dataFile.getTotalSpace();
long sdsize = sdFile.getTotalSpace();
17
//相当于nsuserDefault
SharedPreferences sp = getSharedPreferences("config",MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();;
editor.putBoolean("status",true);
editor.commit();
sp.getBoolean("
status",false);
18 checkbox事件
box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
}
});
19空布局代码加控件展示
scrollow不像ios设置contentSize就可以滚动,添加控件不用写坐标自然就挨个向下排
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/lll"
android:layout_width="368dp"
android:layout_height="495dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></LinearLayout>
</ScrollView>
private LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = findViewById(R.id.lll);
show(ll);
}
public void show(View view){
for (int i=0 ; i < 50 ; i ++){
TextView tv = new TextView(this);
tv.setText("hhh"+i);
tv.setTextColor(Color.RED);
tv.setTextSize(20);
ll.addView(tv);
}
}