new Handler(Looper.getMainLooper()).post({}} 操作布局
android:usesCleartextTraffic="true" //明文传输
允许使用浏览器
<uses-permission android:name="android.permission.INTERNET"/>
允许使用网络
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<activity android:name=".view.HomeView"/> 加载文件
文件跳转
Intent intent = new Intent(MainActivity.this, HomeView.class);
startActivity(intent);
finish(); // 销毁当前Activity
弹窗
Toast.makeText(MainActivity.this, "验证码错误",Toast.LENGTH_SHORT).show();
hint 操作显示默认的东西
随机数
Random random = new Random();
int i = random.nextInt(10000);
返回上一级
super.onBackPressed();
动态布局
LayoutInflater inflater = LayoutInflater.from(this);
GridView parent = findViewById(R.id.constraintLayout3);
parent.removeAllViews();
parent.addView(inflater.inflate(R.layout.index1,parent,false));
设置只允许使用3个字符和只可以输入数字类型
android:inputType="number"
android:maxLength="3"
多选框
<RadioGroup
android:id="@+id/radioGroup2"
>
<RadioButton
android:checked="true"
android:text="男" />
<RadioButton
android:text="女" />
</RadioGroup>
获取多选的按钮// 获取按钮组选中的值
RadioGroup radioGroup = findViewById(R.id.radioGroup2);
RadioButton r1= findViewById(radioGroup.getCheckedRadioButtonId());
设置下拉框数据
Spinner spinner2 = findViewById(R.id.spinner_location2);
String[] data = {"c1", "c2", "c3", "Item 4", "Item 5"};
// 创建一个ArrayAdapter并设置数据源
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner2.setAdapter(adapter);
//获取选中的值
String selectedItem = (String) spinner2.getSelectedItem();
下面的框:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/index"
android:title="Index"
android:icon="@drawable/home"
/>
</menu>
布局
public class UserFragment extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.user_f,container,false);
}
}
路由跳转
BottomNavigationView bot= findViewById(R.id.bot);
if(savedInstanceState==null){
getSupportFragmentManager().beginTransaction().replace(R.id.f1,new indexFragment()).commit();
}
bot.setOnItemSelectedListener(item -> {
Fragment fragment=null;
switch (item.getItemId()){
case R.id.index:
fragment=new indexFragment();
break;
case R.id.shoppy:
fragment=new ShoppyFragment();
break;
case R.id.user:
fragment=new UserFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.f1,fragment).commit();
return true;
});
请求
public static String post(String key, RequestBody body) throws IOException {
User zs = new User("admin", "1232");
JSONObject s1 = new JSONObject();
try {
s1.put("username", zs.getUsername());
s1.put("password", zs.getPassword());
}catch (JSONException e) {
e.printStackTrace();
}
//拼接完整的url
// String path =" http://10.5.127.8:8080/shoppy";
String path ="https://c.anzzm7.top/user";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(path)
// .post(body)
.post(RequestBody.create(MediaType.parse("application/json"), s1.toString())) // 将JSON格式的字符串作为请求体发送
// token验证,也可以使用BirdgeInterceptor拦截器进行设置
// .header("token", user.getString("token", ""))
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response); // 一般都是返回401、404一些异常,可以自己定义方法进行处理
String resp = response.body().string();
return resp;
}
username.setOnFocusChangeListener 失去焦点事件
请求
public static String post(String url, JSONObject js) throws IOException {
//拼接完整的url
String path ="http://text1.00000.work/"+url;
// String path ="https://c.anzzm7.top/user";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(path)
// .post(body)
.post(RequestBody.create(MediaType.parse("application/json"),js.toString())) // 将JSON格式的字符串作为请求体发送
// token验证,也可以使用BirdgeInterceptor拦截器进行设置
// .header("token", user.getString("token", ""))
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response); // 一般都是返回401、404一些异常,可以自己定义方法进行处理
String resp = response.body().string();
return resp;
}
转图片
public static void loadImageFromUrl(String imageUrl, ImageView imageView) {
new Thread(() -> {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream input = connection.getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(input);
// 在 UI 线程更新 ImageView
imageView.post(() -> imageView.setImageBitmap(bitmap));
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
传文件
public static String uploadFileImage(Uri imageUri,InputStream stream ) throws IOException {
File file = new File(imageUri.getPath()); // 替换为您要上传的文件的路径
System.out.println("文件名称"+ file.getName());
String url = "https://file.00000.work:11001/test/upload" ;
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file))
.addFormDataPart("bucketName", "my-bucketname")
.addFormDataPart("userName", "zhangsan")
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
return response.body().string();
}
}
菜单
getSupportFragmentManager().beginTransaction().replace(R.id.linearLayout,new IndexFragment()).commit();
BottomNavigationView bottom = findViewById(R.id.bottom);
bottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment=null;
switch (item.getItemId()) {
case R.id.index:
fragment=new IndexFragment();
break;
case R.id.shoppy:
fragment=new ShoppyFragment();
break;
case R.id.user:
fragment=new UserFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.linearLayout,fragment).commit();
return true;
}
});
文章评论