Android 开发里面如何点击Button将EditText里面的内容显示在textView里

Android 开发里面如何点击Button将EditText里面的内容显示在textView里,第1张

需要获取edittext的内容然后往textview中赋值,具体步骤如下:

1、activity_main.xml中代码如下图,意思是为EditText控件加上背景,这里我们设置了一个android中自带方框的背景,android:background="@android:drawable/editbox_background_normal。

2、获取edittext然后写button按钮,android:hint="用户名",意思是指定了一段提示性的文本。

3、然后指定EditText的最大行数为两行,这样当输入的内容超过两行时,文本就会向上滚动,而EditText则不会再继续拉伸。

4、下图代码写入点击Button将EditText里面的内容显示在textView里。

5、最后测试,下图为点击效果图。

EditText跟一个Button横着排,然后EditText的background="@null", 设置EditText跟Button所在layout的background一个中间透明的边框图片!

如果您对我的回答有不满意的地方,还请您继续追问;

答题不易,互相理解,互相帮助!

这上面的是实现效果:

现在来分析下:

EditText是Android中用来输入内容的控件,要在其内部放置ImgeButton

其实就是布局的问题了

应该使用相对布局RelativeLayout ,因为使用RelativeLayout 布局可以根据android:layout_alignRight="@id/"来控制组件与其他组件之间的关系是怎样的,就是组件与组件之间的相对位置

RelativeLayout子控件的一些属性:

   //相对于同级控件对齐方式

android:layout_alignBaseline将该控件的baseline与给定ID的baseline对齐

android:layout_alignTop 将该控件的顶部边缘与给定ID的顶部边缘对齐

android:layout_alignBottom将该控件的底部边缘与给定ID的底部边缘对齐

android:layout_alignLeft       将该控件的左边缘与给定ID的左边缘对齐

android:layout_alignRight     将该控件的右边缘与给定ID的右边缘对齐

// 相对于父组件对齐方式

android:layout_alignParentTop      如果为true,将该控件的顶部与其父控件的顶部对齐

android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐

android:layout_alignParentLeft      如果为true,将该控件的左部与其父控件的左部对齐

android:layout_alignParentRight    如果为true,将该控件的右部与其父控件的右部对齐

// 居中

android:layout_centerHorizontal 如果为true,将该控件的置于水平居中 

android:layout_centerVertical     如果为true,将该控件的置于垂直居中 

android:layout_centerInParent   如果为true,将该控件的置于父控件的中央 

// 控件离上下左右的像素距离

android:layout_marginTop      上偏移的值 

android:layout_marginBottom 下偏移的值 

android:layout_marginLeft 左偏移的值 

android:layout_marginRight 右偏移的值

//控件相对同级控件的位置

android:layout_toLeftOf在ID控件的左边

android:layout_toRightOf在ID控件的右边

android:layout_below在ID控件的下边

android:layout_above在ID控件的上边

 <RelativeLayout 

       android:layout_marginTop="30dp"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       >

       <EditText

           android:id="@+id/login_password"

           android:layout_width="fill_parent"

           android:layout_height="40dp"

           android:hint="请输入密码"

           android:password="true"

           android:textAppearance="?android:attr/textAppearanceMedium"

           android:singleLine="true" 

           android:ellipsize="end"

           android:paddingRight="5dp"/>

       <ImageButton 

           android:id="@+id/see_password"

           android:layout_width="40dp"

           android:layout_height="25dp"          

           android:background="@drawable/eyes"

           android:layout_margin="10dp"

           android:layout_alignRight="@id/login_password"

           />

   </RelativeLayout>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11769072.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存