FeedbackActivity

package cn.kingvcn.smartcity.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.JsonObject;

import cn.kingvcn.smartcity.R;
import cn.kingvcn.smartcity.base.Response;
import cn.kingvcn.smartcity.http.RetrofitHelper;
import cn.kingvcn.smartcity.utils.SpUtil;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;

public class FeedbackActivity extends AppCompatActivity {

    private ImageView imgBack;
    private EditText editWord;
    private TextView tvWordTotal;
    private TextView tvWordCount;
    private Button btnOk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        initView();
    }

    private void initView() {
        imgBack = findViewById(R.id.img_back);
        editWord = findViewById(R.id.edit_word);
        tvWordTotal = findViewById(R.id.tv_word_total);
        tvWordCount = findViewById(R.id.tv_word_count);
        btnOk = findViewById(R.id.btn_ok);

        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        editWord.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvWordCount.setText(String.valueOf(s.length()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                feedback();
            }
        });
    }

    private void feedback(){
        String content = editWord.getText().toString();
        if (content.length() == 0){
            Toast.makeText(this, "请输入反馈内容", Toast.LENGTH_SHORT).show();
            return;
        }
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("content", content);
        jsonObject.addProperty("title", "意见反馈");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
                jsonObject.toString());
        String token = SpUtil.getToken(this);
        RetrofitHelper.getAppApi().feedback("Bearer " + token, requestBody)
                .enqueue(new Callback<Response>() {
                    @Override
                    public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
                        if (!response.isSuccessful()){
                            return;
                        }
                        Response r = response.body();
                        if (!"200".equals(r.code)){
                            Toast.makeText(FeedbackActivity.this,
                                    "反馈失败:" + r.msg, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        Toast.makeText(FeedbackActivity.this,
                                "反馈成功!", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<Response> call, Throwable throwable) {
                        Toast.makeText(FeedbackActivity.this,
                                "反馈失败:" + throwable.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
    }
}

<?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"
    tools:context=".activity.ChangePwdActivity">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:background="@color/main">

        <ImageView
            android:id="@+id/img_back"
            android:layout_width="20dp"
            android:layout_height="match_parent"
            android:layout_marginStart="16dp"
            android:src="@mipmap/back_white"/>

        <TextView
            android:layout_width="160dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:maxLines="1"
            android:background="@color/main"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:text="意见反馈"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="18dp">

        <EditText
            android:id="@+id/edit_word"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_marginTop="12dp"
            android:background="@drawable/home_edit_search"
            android:gravity="start"
            android:maxLength="150"
            android:paddingLeft="10dp"
            android:paddingTop="12dp"
            android:paddingRight="10dp"
            android:paddingBottom="35dp"
            android:textColor="#2e2e2e"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/tv_word_total"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/edit_word"
            android:layout_alignParentEnd="true"
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="18dp"
            android:text="/150"
            android:textColor="#999999"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/tv_word_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/edit_word"
            android:layout_marginBottom="10dp"
            android:layout_toStartOf="@id/tv_word_total"
            android:text="0"
            android:textColor="#ff0000"
            android:textSize="12sp" />

    </RelativeLayout>

    <Button
        android:id="@+id/btn_ok"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="提交" />
</LinearLayout>

上一篇:个人界面(5分钟教学)


下一篇:设计模式---抽象工厂模式