`

常用控件:TextView EditView

 
阅读更多
TextView



布局:

Xml代码 
<TextView 
        android:id="@+id/textview" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:text="@string/hello_world" 
        tools:context=".MainActivity" /> 

<TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />


调用:

Java代码 
protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        mTextView=(TextView)findViewById(R.id.textview);获得控件  
        mTextView.setText("我的第一个文本");设置文本内容  
            mTextView.setTextColor(Color.GREEN);设置字体颜色  
            mTextView.setBackgroundColor(color.black);}设置背景 <SPAN style="COLOR: #ff0000">这些也可在xml中设置</SPAN> 

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView=(TextView)findViewById(R.id.textview);获得控件
mTextView.setText("我的第一个文本");设置文本内容
        mTextView.setTextColor(Color.GREEN);设置字体颜色
        mTextView.setBackgroundColor(color.black);}设置背景 这些也可在xml中设置







当出现URL E-mail 电话号码时,可以为TextView设置链接:

四种方法实现:

Xml代码 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
<!-- xml属性实现,添加  android:<SPAN style="COLOR: #ff0000">autoLink="all"</SPAN>实现,为所有种类添加链接--> 
    <TextView   
        android:id="@+id/tv01"          
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
       <SPAN style="COLOR: #ff0000"> android:autoLink="all"</SPAN> 
        android:text="@string/first_link" 
        />     
<!--通过<a>标签的string资源文件实现--> 
    <TextView   
        android:id="@+id/tv02" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/second_link" 
        /> 
<!-- 通过在java代码中使用html实现 -->      
    <TextView 
        android:id="@+id/tv03" 
        android:layout_width="fill_parent" 
        android:layout_height="match_parent"         
        /> 
<!-- 通过java代码直接实现 --> 
    <TextView 
        android:id="@+id/tv04" 
        android:layout_width="fill_parent" 
        android:layout_height="match_parent"         
        />                       
</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<!-- xml属性实现,添加  android:autoLink="all"实现,为所有种类添加链接-->
    <TextView
        android:id="@+id/tv01"       
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoLink="all"
        android:text="@string/first_link"
        />  
<!--通过<a>标签的string资源文件实现-->
    <TextView
        android:id="@+id/tv02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/second_link"
        />
<!-- 通过在java代码中使用html实现 -->   
    <TextView
        android:id="@+id/tv03"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"      
        />
<!-- 通过java代码直接实现 -->
    <TextView
        android:id="@+id/tv04"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"      
        />                    
</LinearLayout>


Java代码 
public class MainActivity extends Activity {  
 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
    //设置textview可点击,实现第二种方式   
    TextView t2=(TextView)findViewById(R.id.tv02);  
    t2.setMovementMethod(LinkMovementMethod.getInstance());  
    //使用第三种方式实现  
    TextView t3=(TextView)findViewById(R.id.tv03);  
    t3.setText(  
            Html.fromHtml("<b>text3:</b>"+"<a href=\"http://www.google.com\">连接到google</a>")  
            );  
    t3.setMovementMethod(LinkMovementMethod.getInstance());  
      
    //创建一个spannablestring对象  
    SpannableString ss=new SpannableString("text4:点击这里拨打电话,点击这里链接到google");  
      
    ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,   
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//0-6个字符为粗体  
    ss.setSpan(new URLSpan("tel:415113464"), 9, 11,   
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//9-11为拨号链接  
    ss.setSpan(new URLSpan("http://www.google.com"), 18, 20,  
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//18-20为网站链接  
    ss.setSpan(new BackgroundColorSpan(Color.RED), 23, 29,   
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//23-29为红色  
    TextView t4=(TextView)findViewById(R.id.tv04);  
    t4.setText(ss);  
    //实现第四种方式  
    t4.setMovementMethod(LinkMovementMethod.getInstance());  
      
    }} 

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//设置textview可点击,实现第二种方式
TextView t2=(TextView)findViewById(R.id.tv02);
t2.setMovementMethod(LinkMovementMethod.getInstance());
//使用第三种方式实现
TextView t3=(TextView)findViewById(R.id.tv03);
t3.setText(
Html.fromHtml("<b>text3:</b>"+"<a href=\"http://www.google.com\">连接到google</a>")
);
t3.setMovementMethod(LinkMovementMethod.getInstance());

//创建一个spannablestring对象
SpannableString ss=new SpannableString("text4:点击这里拨打电话,点击这里链接到google");

ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//0-6个字符为粗体
ss.setSpan(new URLSpan("tel:415113464"), 9, 11,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//9-11为拨号链接
ss.setSpan(new URLSpan("http://www.google.com"), 18, 20,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//18-20为网站链接
ss.setSpan(new BackgroundColorSpan(Color.RED), 23, 29,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//23-29为红色
TextView t4=(TextView)findViewById(R.id.tv04);
t4.setText(ss);
//实现第四种方式
t4.setMovementMethod(LinkMovementMethod.getInstance());

}}


String:

Xml代码 
<string name="first_link"> 
  <b>第一种方式</b> 
      通过xml属性实现的链接:www.google.cn,  
      电话:12345645647  
  </string> 
  <string name="second_link"> 
      <b>第二种方式</b> 
      <a href="http://www.google.com">google</a> 
  </string> 

<string name="first_link">
   <b>第一种方式</b>
       通过xml属性实现的链接:www.google.cn,
       电话:12345645647
   </string>
   <string name="second_link">
       <b>第二种方式</b>
       <a href="http://www.google.com">google</a>
   </string>





EditText:

android:hint                         编辑框空是显示的字符

android:textColorHint           编辑框空时显示字符的颜色

android:inputType                 限制输入内容的类型,number,text等

android:digits                        限制输入内容,只可取制定的字符

android:maxLenth                 限制输入的最长字符数

android:inputType="textPassword"    输入密码模式



妈呀,一个英文的引号,写成了中文的引号了,结果出了其它一堆错误,怎么改都不对。

Xml代码 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
 
    <TextView        
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/yh" 
        /> 
     
    <EditText 
        android:id="@+id/et"       
        android:textColorHint="#ff2323" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:hint="@string/yonhuming"/> 
      
    <TextView   
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/mm" 
        /> 
    <EditText   
        android:id="@+id/et1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:inputType="textPassword" 
        android:hint="@string/mima" 
        /> 
    <TextView 
        android:id="@+id/tv" 
        android:layout_width="fill_parent" 
        android:layout_height="match_parent" 
        android:textSize="20sp"          
        />              
</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView     
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/yh"
        />
  
    <EditText
        android:id="@+id/et"    
        android:textColorHint="#ff2323"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/yonhuming"/>
   
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/mm"
        />
    <EditText
        android:id="@+id/et1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="@string/mima"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:textSize="20sp"       
        />           
</LinearLayout>




Java代码 
package example.first;  
 
import android.os.Bundle;  
import android.view.KeyEvent;  
import android.view.View;  
import android.view.View.OnKeyListener;  
import android.widget.EditText;  
import android.widget.TextView;  
import android.app.Activity;  
 
public class MainActivity extends Activity {  
     private EditText et;  
     private EditText et1;  
     private TextView tv;  
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        et=(EditText)findViewById(R.id.et);  
        et1=(EditText)findViewById(R.id.et1);  
        tv=(TextView)findViewById(R.id.tv);  
          
        //设置监听器  
        et1.setOnKeyListener(new OnKeyListener()  
        {  
            @Override 
            public boolean onKey(View v,int keyCode,KeyEvent event){  
                if(keyCode==KeyEvent.KEYCODE_ENTER){  
                    tv.setText("您的用户名为:"+et.getText().toString()+  
                            "\n"+"您的密码为:"+et1.getText().toString());  
                }  
                return false;  
            }  
        });  
    }  
          


package example.first;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {
     private EditText et;
     private EditText et1;
     private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et=(EditText)findViewById(R.id.et);
et1=(EditText)findViewById(R.id.et1);
tv=(TextView)findViewById(R.id.tv);

//设置监听器
et1.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v,int keyCode,KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_ENTER){
tv.setText("您的用户名为:"+et.getText().toString()+
"\n"+"您的密码为:"+et1.getText().toString());
}
return false;
}
});
}

}


运行结果:点击回车后显示用户名和密码









分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics