百度编辑器 编辑 xml 内容丢失

08 112016

1、错误:百度编辑器添加代码(xml)提交后前台显示正常,再次编辑 内容确实

2、原因:

内容显示使用的是:<textarea id="editor" name="content"></textarea>

3、解决方法:

使用官方推荐:<script id="editor" name="content" type="text/plain"></script>


Android 监听EditText 变化

08 112016

1、实现效果:当注册登陆时文本框内容长度大于1时出现 一个全部清空按钮。如图:


blob.png

2、实现思路:实时监听EditText内容变化,当长度大于1,设置ImageView显示,当长度为0,ImageView隐藏


3、具体代码:


//Acitivty代码

public class LoginActivity extends AppCompatActivity implements TextWatcher{

    private EditText _login_phone;
    private EditText _login_pwd;
    private ImageView _login_iv_1;
    private ImageView _login_iv_2;

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

        _login_phone = (EditText)findViewById(R.id.login_phone);
        _login_pwd   = (EditText)findViewById(R.id.login_pwd);
        _login_iv_1  = (ImageView)findViewById(R.id.login_iv_1);
        _login_iv_2  = (ImageView)findViewById(R.id.login_iv_2);

        _login_phone.addTextChangedListener(this);
        _login_pwd.addTextChangedListener(this);


    }
    
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }
    @Override
    public void afterTextChanged(Editable editable) {
        
        //login_iv_1
        if(_login_phone.getText().length() >= 1){    //获取内容长度
            _login_iv_1.setVisibility(View.VISIBLE);  //显示
        }else{
            _login_iv_1.setVisibility(View.INVISIBLE); //隐藏
        }
        
        //login_iv_2
        if(_login_pwd.getText().length() >= 1){
            _login_iv_2.setVisibility(View.VISIBLE);
        }else{
            _login_iv_2.setVisibility(View.INVISIBLE);
        }    
        
    }

    @Override
    public void onClick(View view) {
            
    }

}


//Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@color/colorBg"
    tools:context=".RegisterActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:background="@color/colorWhite"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/login_phone"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:paddingLeft="10dp"
            android:background="@null"
            android:singleLine="true"
            android:inputType="phone"
            android:hint="请输入手机号"/>

        <ImageView
            android:id="@+id/login_iv_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_pwd_delete"
            android:layout_gravity="center"
            android:visibility="invisible"
            android:layout_marginRight="8dp"/>

    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:background="@color/colorWhite"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/login_pwd"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="10dp"
            android:layout_weight="3"
            android:singleLine="true"
            android:background="@null"
            android:inputType="textPassword"
            android:hint="请输入密码"/>

        <ImageView
            android:id="@+id/login_iv_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_pwd_delete"
            android:layout_gravity="center"
            android:visibility="invisible"
            android:layout_marginRight="8dp"/>

    </LinearLayout>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="50dp"
        android:orientation="horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center|center_horizontal|center_vertical"
        >

        <Button
            android:id="@+id/button_login"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="20dp"
            android:textColor="@color/colorWhite"
            android:textStyle="normal"
            android:layout_gravity="center"
            android:background="@drawable/register_button_selector"
            android:text="登录"/>

    </LinearLayout>


</LinearLayout>



java.lang.RuntimeException: Can't create handler inside thread t

08 102016
//错误提示如下:

  FATAL EXCEPTION: Thread-166
 Process: com.a1m85.yima, PID: 23476
 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
 at android.os.Handler.(Handler.java:200)
 at android.os.Handler.(Handler.java:114)
 at android.widget.Toast$TN.(Toast.java:345)
 at android.widget.Toast.(Toast.java:101)
 at android.widget.Toast.makeText(Toast.java:259)
 at com.a1m85.yima.RegisterActivity$1$override.run(RegisterActivity.java:138)
 at com.a1m85.yima.RegisterActivity$1$override.access$dispatch(RegisterActivity.java)
 at com.a1m85.yima.RegisterActivity$1.run(RegisterActivity.java:0)
 at java.lang.Thread.run(Thread.java:818)
 
//在Android中不允许Activity里新启动的线程访问该Activity里的UI组件,这样会导致新启动的线程无法改变UI组件的属性值。


Android发送Post请求

08 082016
//Android发送Post请求
Thread thread = new Thread(new Runnable() {
    
    //请求服务器地址
    String server = "http://192.168.1.102/index.php/index/member/register";
    @Override
    public void run() {
    
        try {
                //new url
            URL url = new URL(server);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                                   
            conn.setRequestMethod("POST");  //Post请求
            conn.setDoInput(true);   
            conn.setDoOutput(true);

            String param = "username=15612341153&pwd1=123&pwd2=123";
            DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(param);

            InputStream in = conn.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(in));

            String s1;
            String s2 = "";
            while((s1=bf.readLine()) != null){
                s2 = s2 + s1;
            }

            //Toast something

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
});

thread.start();