Reject Message 實現-1

Reject Message(拒接短信) 使用ListView存儲用戶預設的短信,在來電中可以使用,較爲簡單也常用。

本文主要介紹Layout及簡單實現添加和刪除,並未涉及存儲及應用(待完善)。

涉及到的基本知識:各種佈局管理器的使用、ListView使用BaseAdapter的實現、ActionBar樣式的重寫等等

 

直接上代碼:

(1) java 文件

package com.xx.myrejectmessageapp;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class MyRejectMessageActivity extends Activity {
    public final String LOG_TAG = "MyRejectMessageActivity";
    private List<RejectMessageData> data = new ArrayList<RejectMessageData>();
    private RejectMessageAdapter adapter = new RejectMessageAdapter();
    private ListView listView;
    private ImageButton addButton;
    //private ImageButton delButton;
    private EditText inputNumber;
    private TextView emptyTextView;

    private ActionBar actionBar;

    public class RejectMessageData{
        private String number;
        private String name;

        public RejectMessageData(String number, String name) {
            this.number = number;
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getNumber() {
            return number;
        }

        public void setNumber(String number) {
            this.number = number;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_reject_message);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        listView = (ListView) findViewById(R.id.rejectMessageListView);
        addButton = (ImageButton) findViewById(R.id.addButton);
        inputNumber = (EditText) findViewById(R.id.enterNumber);
        emptyTextView = (TextView) findViewById(R.id.empty);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = null;
                number = inputNumber.getText().toString();
                if(number != null){
                    RejectMessageData inputData = new RejectMessageData(number, "no name");
                    data.add(inputData);
 //                   emptyTextView.setVisibility(View.GONE);
 //                   listView.setVisibility(View.VISIBLE);
 //                   listView.setAdapter(adapter);
                    updateRejectMessage();
                }
            }
        });

        actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
}


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.menu_my_reject_message, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }
        if(id == android.R.id.home){
            onBackPressed();
        }

        return super.onOptionsItemSelected(item);
    }

    public class RejectMessageAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewGroup rootView = null;
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rootView = (ViewGroup) inflater.inflate(R.layout.reject_message_list_item, rootView, true);
            TextView numberText;
            TextView nameText;
            ImageButton delButton;
            numberText = (TextView) rootView.findViewById(R.id.reject_message_item_number);
            nameText = (TextView) rootView.findViewById(R.id.reject_message_item_name);
            delButton = (ImageButton) rootView.findViewById(R.id.delete);
            numberText.setText(data.get(position).getNumber());
            nameText.setText(data.get(position).getName());

            delButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    data.remove(position);
                    updateRejectMessage();
                }
            });

            return rootView;
        }
    }

    public void updateRejectMessage(){
        if(data.size() != 0) {
            emptyTextView.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
        }else{
            emptyTextView.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
        }

        listView.setAdapter(adapter);
    }
}

(2)界面主要佈局文件:activity_my_reject_message.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:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:orientation="vertical"
    tools:context=".MyRejectMessageActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="87dp"
        android:paddingTop="18dp">
        <ImageButton
            android:id="@+id/addButton"
            android:layout_width="@dimen/add_delete_button_width"
            android:layout_height="@dimen/add_delete_button_height"
            android:src="@drawable/tw_list_icon_create_mtrl"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_marginRight="48dp">
            <TextView
                android:id="@+id/editTextTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Add call-reject message" />
            <EditText
                android:id="@+id/enterNumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:hint="Enter number" />
        </LinearLayout>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="16dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/logButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Logs" />
        <Button
            android:id="@+id/contactsButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="contacts" />
    </LinearLayout>

    <View
        android:id="@+id/reject_divider"
        android:layout_width="match_parent"
        android:layout_height="2px"
        android:background="@color/list_divider_color"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="empty"
            android:textColor="@color/empty_text_color"
            android:visibility="visible" />
        <ListView
            android:id="@+id/rejectMessageListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"></ListView>
    </FrameLayout>

</LinearLayout>



(3) list item 佈局文件 reject_message_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/reject_message_item_height"
    android:minHeight="?android:attr/listPreferredItemHeight" >

    <ImageButton
        android:id="@+id/delete"
        android:layout_width="@dimen/add_delete_button_width"
        android:layout_height="@dimen/add_delete_button_height"
        android:src="@drawable/tw_list_icon_minus_mtrl"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="48dp"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true" >

        <TextView
            android:id="@+id/reject_message_item_number"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="@dimen/reject_message_item_text_size1"
            android:textColor="@color/reject_message_item_text_color1"/>

        <TextView
            android:id="@+id/reject_message_item_name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="@dimen/reject_message_item_text_size2"
            android:textColor="@color/reject_message_item_text_color2"/>
    </LinearLayout>
</RelativeLayout>

 

(4)  AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xx.myrejectmessageapp" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/RejectMessageApp" >
        <activity
            android:name=".MyRejectMessageActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

(5) styles.xml

<resources>

    <!-- Base application theme, if using AppTheme, not work -->
    <style name="RejectMessageApp" parent="@android:style/Theme.DeviceDefault.Light">
        <!-- Customize your theme here. -->

        <item name="android:statusBarColor">@color/status_bar_color</item>
        <item name="android:colorPrimary">@color/action_bar_color</item>
        <item name="android:textColorPrimary">@color/action_bar_text_color</item>
    </style>

    <style name="reject_message_actionbar_style" parent="@android:style/Theme.DeviceDefault.Light">
        <item name="android:background">@color/action_bar_color</item>
    </style>

    <!-- tmp theme. -->
    <style name="Theme.Setting" parent="@android:style/Theme.DeviceDefault.Light">
        <!-- Customize your theme here. -->
        <item name="android:statusBarColor">@color/status_bar_color</item>
        <item name="android:actionBarStyle">@style/reject_message_actionbar_style</item>
    </style>

</resources>

 

(6) colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="empty_text_color">#47952b</color>
    <color name="action_bar_color">#4fa630</color>
    <color name="status_bar_color">#3f8526</color>
    <color name="action_bar_text_color">#fafafa</color>
    <color name="reject_message_item_text_color1">#000000</color>
    <color name="reject_message_item_text_color2">#838688</color>
    <color name="list_divider_color">#cecece</color>
</resources>

(7) dimens.xml

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="reject_message_item_height">80dp</dimen>
    <dimen name="add_delete_button_height">32dp</dimen>
    <dimen name="add_delete_button_width">32dp</dimen>
    <dimen name="reject_message_item_text_size1">17sp</dimen>
    <dimen name="reject_message_item_text_size2">14sp</dimen>
</resources>

 

 總結點:

1.  在item的layout文件中,用android:layout_height設置item的高度。 運行,高度設置無效。
解決辦法:給item設定minHeight,即可.
e.g : android:minHeight="?android:attr/listPreferredItemHeight"

2. 更改statusbar / actionbar 背景顏色
AndroidManifest.xml -> application 對應的android.theme="@style/MyAppTheme"

-> styles.xml 定義的AppTheme 改爲:
<style name="MyAppTheme" parent="@android:style/Theme.DeviceDefault.Light" >
    <item name="android:statusBarColor">#3f8526</item>
 <item name="android:ActionBarStyle">@style/my_actionbar_style</item>
</style>

<style name="my_actionbar_style" parent="@android:style/Theme.DeviceDefault.Light >
    <item name="android:background">#4fa630</item>
</style>

// 注意儘量不要使用AppTheme命名,可能會修改無效!!!

PS: 更改ActionBar顏色
android:colorPrimary
android:textColorPrimary -->標題

 

next : 實現可以存儲,即退出應用再進入能保存之前設置的數據。

發佈了31 篇原創文章 · 獲贊 10 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章