Android 布局之 TableLayout

08 182016

1、特点:

表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象。TableRow可以添加子控件,每添加一个为一列。

2、属性:

android:collapseColumns:将TableLayout里面指定的列隐藏,若有多列需要隐藏,
请用逗号将需要隐藏的列序号隔开。             
android:stretchColumns:设置指定的列为可伸展的列,以填满剩下的多余空白空间,
若有多列需要设置为可伸展,请用逗号将需要伸展的列序号隔开。                
android:shrinkColumns:设置指定的列为可收缩的列。当可收缩的列太宽(内容过多)不会被挤出屏幕。
当需要设置多列为可收缩时,将列序号用逗号隔开。列元素(Button)属性:(奇怪的是button 
里面没有android:layout_column 和android:layout_span两个属性,写进去无反应,还不知道为什么)
android:layout_colum:设置该控件在TableRow中指定的列。
android:layout_span:设置该控件所跨越的列数。


Android 数据存储之SQLite

08 162016
//官方提供了 抽象类 SQLiteOpenHelper
public abstract SQLiteOpenHelper extends Object

什么是抽象类?

所以我们需要自定义一个工具类继承自SQLiteOpenHelper

public class MySqliteHelper extends SQLiteOpenHelper {

    public MySqliteHelper(Context context) {
        super(context, "test", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
            String sql = "Create table Book (id integer primary key autoincrement, author text)";
            sqLiteDatabase.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

//activity中
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MySqliteHelper mySqliteHelper = new MySqliteHelper(this);

        SQLiteDatabase db = mySqliteHelper.getWritableDatabase();

        String sql = "INSERT INTO Book (id, author)VALUES(231,2)";
        db.execSQL(sql);
        db.close();

    }
}


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();