最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】实现安卓版的DownloadSongtasteMusic中的添加文本标签和文本输入框

Android crifan 2869浏览 0评论

之前已经创建好了项目:

【记录】创建第一个Android程序: Building Your First App

创建了AVD:

【记录】创建AVD(Android Virtual Device)安卓虚拟设备

运行了app:

【记录】ADT中通过安卓虚拟设备运行安装程序

现在,接着就是去,实现安卓版的DownloadSongtasteMusic中的,文本标签,和输入框了。


1.参考:

Building a Simple User Interface

去看看,如何创建对应的简单的UI。

2.双击src/layout/activity_main.xml,默认是通过Graphical Layout打开的,点击以切换到activity_main.xml的文本编辑模式:

xml editor for activity xml file

2.再参考教程,把代码改为:

<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="horizontal"
    tools:context=".MainActivity" >

    <!-- 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
 	-->
 	
</LinearLayout>

linearlayout orientation

 

3.期间,写了如下代码:

 	<EditText 
 	    android:id="@+id/url_or_id"
 	    android:layout_width="wrap_content"
 	    android:layout_height="wrap_content"
 	    android:hint="@string/songtaste URL or ID"
 	    >

结果出现一个警告,暂时记录与此:

【未解决】使用ADT进行Android开发期间,编辑XML时提示:The view name (@+id/xxx) suggests this is a URI, but it does not include ‘textUri’ in the inputType

 

4.然后顺便去看了,对应的R.java中的效果:

new created id in R.java file

可以看到,的确已经同步创建了对应的变量url_or_id了。

5。从教程中的介绍得知,此处用加号,表示新创建的resource

而对于string和layout,是不需要的。(静态时,就已创建好了)

6.看了后面的解释,才知道,上述的:

android:hint="@string/songtaste URL or ID"

写法是错误的,应该是:

android:hint="@string/url_or_id"

其中,此处string下面的url_or_id,和之前:

android:id="@+id/url_or_id"

中的id下面的url_or_id,由于属于不同的scope,可以理解为namespace,

所以两者并不是同一个变量,而是不同的变量(资源);

对应的,string中的url_or_id,后续代码会另外定义其值的。

7.再去打开res/values/strings.xml:

strings xml delete hello world

添加一个新的:

add string value

add url_or_id and value

按Ctrl+S,保存一下。

8.接着继续去编辑xml文件,添加变量btn_download:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Download Songtaste Music</string>
    <string name="menu_settings">Settings</string>
    <string name="url_or_id">Input Songtaste URL or ID</string>
    <string name="btn_download">Download</string>

</resources>

期间遇到一个错误,详情参考:

【已解决】ADT中编辑strings.xml时出现错误:content is not allowed in prolog


9.目前的代码是:

/DownloadSongtasteMusic/res/layout/activity_main.xml

<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="horizontal"
    tools:context=".MainActivity" >

    <!-- 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
 	-->
 	
 	<EditText 
 	    android:id="@+id/url_or_id"
 	    android:layout_width="wrap_content"
 	    android:layout_height="wrap_content"
 	    android:hint="@string/url_or_id" />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_download"
        />

</LinearLayout>

 

/DownloadSongtasteMusic/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Download Songtaste Music</string>
    <string name="menu_settings">Settings</string>
    <string name="url_or_id">Input Songtaste URL or ID</string>
    <string name="btn_download">Download</string>

</resources>

然后点击Run,运行的结果是:

show text box and button

10.添加了

android:layout_weight="1"

相关代码变成:

 	<EditText 
 	    android:id="@+id/url_or_id"
 	    android:layout_width="wrap_content"
 	    android:layout_height="wrap_content"
 	    android:hint="@string/url_or_id"
 	    android:layout_weight="1" />"

然后Run,结果是:

text box smaller

11.再改为0dp:

 	<EditText 
 	    android:id="@+id/url_or_id"
 	    android:layout_width="0dp"
 	    android:layout_height="wrap_content"
 	    android:hint="@string/url_or_id"
 	    android:layout_weight="1" />"

运行后效果,和之前一样:

after 0dp

 

【总结】

1.通过

res/layout/activity_main.xml

控制,有什么:

文本输入框

按钮

等等;

其中不同的layout方面的参数配置,决定了位置,宽度,高度等显示内容;

2.通过

res/values/strings.xml

去设置对应的string变量值。

转载请注明:在路上 » 【记录】实现安卓版的DownloadSongtasteMusic中的添加文本标签和文本输入框

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.179 seconds, using 22.12MB memory