Android Studio 基础代码总结

(请求 大集合里套小集合)

List<Map<String,Object>>list= (List<Map<String, Object>>) map.get("data");

    data=new ArrayList<>();

    for (Map<String,Object>a:list){

    Map<String,Object>b= (Map<String, Object>) a.get("nextStep");

———————————————————————————————

(网络请求视频)

//先获取视频链接.listview)   把 VideoView放进bean.get里面

v1.setVideoURI(Uri.parse(   bean.getShipin()) );

v1.start();

———————————————————————————————

(RadioButto)赋值    直接被选择

if (sex.equals("1")){

    r1.setChecked(true);

}else if (sex.equals("0")){

    r2.setChecked(true);

}

———————————————————————————————

(当RadioButton被选择时 则赋值)

使用RadioGroup 点击事件

r r. setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

    public void onCheckedChanged(RadioGroup group, int checkedId) {

        if (checkedId==R.id.r1){

            sex1="1";

        }else if (checkedId==R.id.r2){

            sex1="0";

        }

    }

});

FrameLayout  的使用

//点击后 切换 页面, ReplaceFragment为定义方法

ReplaceFragment (new zhuye( name )

private void ReplaceFragment(Fragment fragment) {

    FragmentManager fragmentManager=getFragmentManager();

    FragmentTransaction transaction=fragmentManager.beginTransaction();

    transaction.replace(R.id.frame2,fragment);

    transaction.commit();

}

//可以传递数据。 name为任意值  点击切换 对应(内容) 时可以使用

———————————————————————————————

Editext   属性

   

          android:ellipsize="end"      //结尾省略号

           android:digits=""      //限制 输入内容

inpute=””

            Max. 设置 字数

———————————————————————————————

按照 id 大小进行排序

Collections.sort(fuwu_data, new Comparator<FuWuBean>() {

    public int compare(FuWuBean o1, FuWuBean o2) {          后减前 从大到小,反之

        return o2.getId() - o1.getId();

    }

});

点击列表信息 任意 一行  使他对应变色 

for (int i = 0; i < parent.getChildCount(); i++) {

    View childView = parent.getChildAt(i);

    childView.setTextColor(Color.TRANSPARENT);

}

view.setTextColor(Color.Red);

———————————————————————————————

listview   gridview  宽高

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int high=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);

 super.onMeasure(widthMeasureSpec,high);

}

———————————————————————————————————————

隐藏密码和显示密码 通过取余 数 来判断 显示隐藏

private int a=0;

b1=root.findViewById(R.id.b1);

b1.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

        a++;

      if (a%2==1) {

          mima.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

      }else if (a%2==0){

          mima.setTransformationMethod(PasswordTransformationMethod.getInstance());

      }

    }

});

———————————————————————————————————————

倒计时

private int num=60;

handler.sendEmptyMessageDelayed(0,1000);

private Handler handler=new Handler(){
    @Override
    public void handleMessage(@NonNull Message msg) {

        num--;

        if (num==-1){
            Toast.makeText(MainActivity.this, "游戏结束", Toast.LENGTH_SHORT).show();
            finish();
        }else {
            t1.setText("还剩"+num+"秒");
            handler.sendEmptyMessageDelayed(0,1000);
        }
    }
};

———————————————————————————————————————

未来七天日期 选择   //String 类型

private List<String>riqi_Data;

riqi = findViewById(R.id.riqi);
riqi_Data = new ArrayList<>();


Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < 7; i++) {
    riqi_Data.add(dateFormat.format(calendar.getTime()));
    calendar.add(Calendar.DAY_OF_MONTH, 1);
}
riqi.setAdapter(new PublicGoneAdapter(riqi_Data, MainActivity.this));

———————————————————————————————————————

对话框  视图文件中

AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
builder.setTitle("贪吃蛇游戏");
builder.setMessage("Game over!");

builder.setPositiveButton("Play again", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        Intent intent=new Intent(getContext(),StartGame.class);
        getContext().startActivity(intent);
        ((Activity) getContext()).finish();   //关闭当前页面
        dialog.dismiss();
    }
});

builder.setNegativeButton("Back to home", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Intent intent=new Intent(getContext(),MainActivity.class);
        getContext().startActivity(intent);
       ( (Activity)getContext()).finish();
        dialog.dismiss();
    }
});

———————————————————————————————————————

本地存储 获取

// 写入数据 SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("key", ""); editor.apply();

// 读取数据 SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE); String value = sharedPreferences.getString("key", "");

———————————————————————————————————————

android stduio sdk 30版本 使用网络 要求

<uses-permission android:name="android.permission.INTERNET"/>

android:usesCleartextTraffic="true"

———————————————————————————————————————

android stduio 自动化测试

androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'

@RunWith(AndroidJUnit4.class)


public class TestCeShi {


    private UiDevice device;

测试之前 打开app


    @Before
    public void  before(){

        Instrumentation instrumentation=getInstrumentation();

        device=UiDevice.getInstance(instrumentation);
        Context context=instrumentation.getTargetContext().getApplicationContext();

        Intent intent=context.getPackageManager().getLaunchIntentForPackage("com.example.zhihuichengshi");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

        context.startActivity(intent);
    }


    @Test
    public void Start() throws UiObjectNotFoundException {

//测试 button 按钮

UiObject button=device.findObject(new UiSelector().resourceId(""));
button.click();

//测试 输入框

UiObject editext=device.findObject(new UiSelector().resourceId(""));

editext.setText(“”);

//测试滑动 ScrowView

UiScrollable scrollable=new UiScrollable(new UiSelector().scrollable(true));


scrollable.scrollForward();   //向下滑
scrollable.scrollBackward();  //向上滑

//测试滑动 listview gridview

UiScrollable listView = new UiScrollable(new UiSelector().resourceId("com.example.zhihuichengshi:id/xinwenliebiao"));
listView.scrollForward();

//点击 列表


    UiObject listItem = device.findObject(new UiSelector().resourceId(“”));

    listItem.click();

// 测试键盘

decive.pressEnter;

}

日期选择器

 riqi = findViewById(R.id.riqi);


 riqi.setVisibility(View.VISIBLE);
 
riqi.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
    public void onDateChanged(DatePicker view, int year, int month, int day) {


        int monthgo=month+1;
        Toast.makeText(MainActivity.this, year+"年"+monthgo+"月"+day+"日", Toast.LENGTH_SHORT).show();

        TextView t1=findViewById(R.id.t1);
        t1.setText(year+"年"+monthgo+"月"+day+"日");
    }
});

———————————————————————————————————————

随机数字

int arr[]={0,1,2,3,4,5,6,7,8,9};
Random random=new Random();

int a = 0;

for (int i = 0; i <4 ; i++) {

    a=random.nextInt(arr.length);
    Log.e("a", String.valueOf(a));

}

———————————————————————————————————————

//实时时间 更新 每一秒更新一次

private Handler handler=new Handler(){
    @Override
    public void handleMessage(@NonNull Message msg) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
        String dateTime = dateFormat.format(new Date());
        t1.setText(dateTime);
        handler.sendEmptyMessageDelayed(0,1000);
    }
};

———————————————————————————————————————

//进度条 加载

 p1=findViewById(R.id.progressBar);
p1.setMax(maxnum);
handler.sendEmptyMessageDelayed(0,1000);
private Handler handler=new Handler(){
    @Override
    public void handleMessage(@NonNull Message msg) {

        num++;
        p1.setProgress(num);
        handler.sendEmptyMessageDelayed(0,1000);

        if (num==100){
            Toast.makeText(MainActivity.this, "加载成功", Toast.LENGTH_SHORT).show();
            p1.setVisibility(View.INVISIBLE);
        }

    }
};

———————————————————————————————————————

//打开系统图库

Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE);
  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小舒卿雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值