Android(1)-自定义控件(输入框)extendsLinearLayout


一.简单的自定义控件,输入框

  新建一个类继承LinearLayout,同时新建一个与之对应的xml文件即可。下面就单纯的贴代码,简单易懂,不解释啦!

CustomEditText.java

package com.example.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.testcustomwidget.R;

public class CustomEditText extends LinearLayout {

    //private ImageView ivEdtIcon;
    private EditText edtEdtInput;
    
    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init(context);
    }
    
    private void init(Context context){
        LayoutInflater.from(context).inflate(R.layout.custom_edittext, this);
        //ivEdtIcon = (ImageView)findViewById(R.id.edt_icon);
        edtEdtInput = (EditText)findViewById(R.id.edt_input);
    }

    public EditText getEditText() {
        return edtEdtInput;
    }
}

custom_edittest.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@android:color/white"
    >
    
    <ImageView 
        android:id="@+id/edt_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/hello_world"
        android:src="@drawable/phone_num_icon"
        />
    <EditText
        android:id="@+id/edt_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hello_world"
        />
    
    
</LinearLayout>

MainActivity.java

package com.example.testcustomwidget;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.widget.CustomEditText;

public class MainActivity extends Activity {

    private CustomEditText cusEdt;
    private ImageButton ibTest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        attachEvent();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void findViews() {
        // TODO Auto-generated method stub
        cusEdt = (CustomEditText)findViewById(R.id.custom_input);
        ibTest = (ImageButton)findViewById(R.id.btnTest);
    }
    
    private void attachEvent() {
        ibTest.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String strGet = cusEdt.getEditText().getText().toString();
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "test:" + strGet, Toast.LENGTH_LONG).show();
            }    
        });
    }
    
}

activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
       android:background="@drawable/login_bg"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <com.example.widget.CustomEditText
        android:id="@+id/custom_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp" 
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        />

    <ImageButton
        android:id="@+id/btnTest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/custom_input"
        android:layout_below="@+id/custom_input"
        android:layout_marginTop="43dp"
        android:layout_marginRight="20dp"
        android:contentDescription="@string/hello_world"
        android:background="@drawable/bg_button"
        android:text="@string/hello_world"
         />

</RelativeLayout>

ok!具体的代码实现就以上这些,简单的实现了自定义控件。效果图如下:

另外,感觉到自己的命名规范不是很好,有意者如有什么高见,请指教,相互交流,谢谢!

 源码地址:http://download.csdn.net/detail/leaf_6057/6721859

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM