关于构建Miwok应用的一些总结,以后开发其他应用时可以用到的一些技能

14 篇文章 0 订阅

1.Application主题的改变,以及视图样式的改变
2.设置view的click监听器,以及使用Intent传递消息
3.利用ListView和自定义适配器来自定义列表项
4.为ListView列表项设置点击事件监听器的另一种方法
5.在设置颜色的时候,尽量不要使用硬编码的形式,而应该在colors.xml中添加颜色resources,以资源id的方式引用,以便后期更改;

1.Application主题的改变,以及视图样式的改变

主题改变在styles.xml中配置,然后在AndroidManifest.xml中的< application>标签中设置android:theme属性

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/primary_color</item>
        <item name="colorPrimaryDark">@color/primary_dark_color</item>
    </style>

    <!-- Style for a category of vocabulary words -->
    <style name="CategoryStyle">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">@dimen/activity_list_item_height</item>
        <item name="android:gravity">center_vertical</item>
        <item name="android:padding">16dp</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textAppearance">@android:style/TextAppearance</item>
    </style>
</resources>

上面第一个< style>是用于application的主题,下面的< style>是用于列表项的主题,以后涉及到某几个TextView或者别的几个视图若是有重复的样式,可以采取这种方法,重新定义一个style。
在AndroidMainifest.xml中设置主题代码如下:

...
    <application
    	...
        android:theme="@style/AppTheme">
        ...
    </application>

在某个视图中设置style的代码如下:

    <TextView
    	...
        style="@style/CategoryStyle"
 		.../>

2.设置view的click监听器,以及使用Intent传递消息

1.设置监听器的方法,主要步骤:
自定义一个扩展自监听器View.OnclickListener的类;在自定义监听器类中重写点击方法onClick;哪个视图要点击,就给哪个视图注册对应的监听器对象,xxx.setOnClickListener。
也可使用创建匿名类的方式直接在注册的过程中自定义一个扩展自View OnclickListener的类。
举个例子:

        TextView numbers = (TextView) findViewById(R.id.numbers);
        numbers.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                Toast.makeText(MainActivity.this, "Opening Numbers Lists", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(MainActivity.this, NumbersActivity.class);
                startActivity(intent);
            }

        });

上述方法中用到了使用匿名类直接实现接口(要注意接口无法进行实例化,但是可以通过匿名类来进行实现)

2.Intent分为显示Intent和隐式Intent,显示Intent通常用在同一应用中。而当不知道用户在设备上安装了哪些应用时,用隐式Intent。
利用显示Intent跳转页面,并在不同Activity中传递数据的例子:

 TextView buyshop_text_view = (TextView)findViewById(R.id.buyshop_text_view);
        buyshop_text_view.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CategoryActivity.this, ShopActivity.class);
                //利用Bundle添加传递数据
                //创建包裹bundle
                Bundle bundle = new Bundle();
                bundle.putString("airportName", airportName);
                bundle.putInt("category", 1);
                //将bundle塞给intent
                intent.putExtras(bundle);
                //启动Intent
                startActivity(intent);
            }
        });

另一个Activity中接收数据:

 		Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        
        String airportName = bundle.getString("airportName","");
        int category = bundle.getInt("category");

3.隐式Intent的例子(用到JustJava项目的例子)

   public void createOrderSummary(int PriceOfOrder) {

        EditText nameEditText = (EditText) findViewById(R.id.name_edit_text);
        String name = nameEditText.getText().toString();
        String message = "Name: " + name + "\n" +
                "Quantity: " + quantity + "\n" +
                "Total: " + "$ " + (quantity * PriceOfOrder + whippedCreamPrice + chocolatePrice) + "\n" +
                "Add whipped cream? " + hasWhippedCream + "\n" +
                "Add chocolate? : " + hasChocolate + "\n" +
                getString(R.string.thank_you);

        String [] addresses = new String[]{"Jack Lee's company"};
        String subject = "Erlich Bachman Coffee order for " + name;
        //设置Intent动作为直接拨号
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        //设置Intent前往的路径
        intent.setData(Uri.parse("mailto:")); // only email apps should handle this
        //添加数据
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, message);
        intent.putExtra(Intent.EXTRA_EMAIL, addresses);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
            Toast.makeText(MainActivity.this, "Order successfully!", Toast.LENGTH_SHORT).show();
        }
    }

4.在隐式Intent中还用到了过滤器的概念,即把不符合匹配条件的过滤掉,AndroidManifest.xml里的< activity>内的intent-filter标签就是过滤器,如:

        <activity android:name=".MainActivity">
            <intent-filter>
            	//表示App的入口动作
                <action android:name="android.intent.action.MAIN" />
				//表示在App启动时调用
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

3.利用ListView和自定义适配器来自定义列表项,具体方法见前几篇总结

利用ListView和自定义适配器来构建自定义列表项,并设置点击事件监听器

4.为列表项设置点击事件监听器的另一种方法

ListView中利用另一方法AdapterView.setOnItemClickListener来设置列表项的点击事件监听器

5.在设置颜色的时候,尽量不要使用硬编码的形式,而应该在colors.xml中添加颜色resources,以资源id的方式引用,以便后期更改;

字符串和视图高度的设置也是同样如此,不要使用硬编码形式而是应该在strings.xml和dimens.xml中设置

colors.xml

<resources>
    <!-- Primary color of the app (shown in the app bar) -->
    <color name="primary_color">#4A312A</color>

    <!-- Primary dark color of the app (shown in the status bar) -->
    <color name="primary_dark_color">#2F1D1A</color>

    <!-- Background color for app -->
    <color name="tan_background">#FFF7DA</color>

    <!-- Background color for the numbers category -->
    <color name="category_numbers">#FD8E09</color>

    <!-- Background color for the family members category -->
    <color name="category_family">#379237</color>

    <!-- Background color for the colors category -->
    <color name="category_colors">#8800A0</color>

    <!-- Background color for the phrases category -->
    <color name="category_phrases">#16AFCA</color>
</resources>

strings.xml

<resources>
    <!-- Title for the application. [CHAR LIMIT=12] -->
    <string name="app_name">Miwok</string>

    <!-- Category name for phrases [CHAR LIMIT=20] -->
    <string name="category_phrases">Phrases</string>

    <!-- Category name for the vocabulary words for colors [CHAR LIMIT=20] -->
    <string name="category_colors">Colors</string>

    <!-- Category name for the vocabulary words for numbers [CHAR LIMIT=20] -->
    <string name="category_numbers">Numbers</string>

    <!-- Category name for the vocabulary words for family members [CHAR LIMIT=20] -->
    <string name="category_family">Family Members</string>
</resources>

dimens.xml

<resources>
    <!-- Height of each list item -->
    <dimen name="list_item_height">88dp</dimen>
    <dimen name="activity_list_item_height">130dp</dimen>

</resources>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值