Android TV 开发(3)

叁叁肆2018-09-26 11:25

本文来自网易云社区

作者:孙有军


  <LinearLayout
        android:id="@+id/input_num_line_3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/input_num_line_2"
        android:baselineAligned="false"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_weight="1">

            <TextView
                android:id="@+id/input_key_number_4"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_centerInParent="true"
                android:background="@drawable/keyboard_item_selector"
                android:focusable="true"
                android:gravity="center"
                android:text="4"
                android:textColor="#ffffff"
                android:textSize="30sp"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_weight="1">

            <TextView
                android:id="@+id/input_key_number_5"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_centerInParent="true"
                android:background="@drawable/keyboard_item_selector"
                android:focusable="true"
                android:gravity="center"
                android:text="5"
                android:textColor="#ffffff"
                android:textSize="30sp"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_weight="1">

            <TextView
                android:id="@+id/input_key_number_6"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_centerInParent="true"
                android:background="@drawable/keyboard_item_selector"
                android:focusable="true"
                android:gravity="center"
                android:text="6"
                android:textColor="#ffffff"
                android:textSize="30sp"/>
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/input_num_line_4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/input_num_line_3"
        android:baselineAligned="false"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_weight="1">

            <TextView
                android:id="@+id/input_key_number_1"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_centerInParent="true"
                android:background="@drawable/keyboard_item_selector"
                android:focusable="true"
                android:gravity="center"
                android:text="1"
                android:textColor="#ffffff"
                android:textSize="30sp"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_weight="1">

            <TextView
                android:id="@+id/input_key_number_2"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_centerInParent="true"
                android:background="@drawable/keyboard_item_selector"
                android:focusable="true"
                android:gravity="center"
                android:text="2"
                android:textColor="#ffffff"
                android:textSize="30sp"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_weight="1">

            <TextView
                android:id="@+id/input_key_number_3"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_centerInParent="true"
                android:background="@drawable/keyboard_item_selector"
                android:focusable="true"
                android:gravity="center"
                android:text="3"
                android:textColor="#ffffff"
                android:textSize="30sp"/>
        </RelativeLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/show_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/input_num_line_4"
        android:layout_centerInParent="true"
        android:padding="@dimen/gap_20_dp"
        android:textColor="#ffffff"
        android:textSize="33sp"/>
</RelativeLayout>

对应的界面代码如下:

public class DialPanFragment extends Fragment implements View.OnClickListener {

    private TextView showPhone;

    private ImageView dialBnt;

    public DialPanFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_dial_pan, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        findViews();
    }

    private void findViews() {
        showPhone = (TextView) getView().findViewById(R.id.show_phone);
        dialBnt = (ImageView) getView().findViewById(R.id.dial_icon);
        dialBnt.setOnClickListener(this);
        dialBnt.setTag(-2);
        dialBnt.setEnabled(false);
        View view0 = getView().findViewById(R.id.input_key_number_0);
        view0.setTag(0);
        view0.setOnClickListener(this);
        View view1 = getView().findViewById(R.id.input_key_number_1);
        view1.setTag(1);
        view1.setOnClickListener(this);
        view1.setNextFocusUpId(R.id.dial_tab);
        View view2 = getView().findViewById(R.id.input_key_number_2);
        view2.setTag(2);
        view2.setOnClickListener(this);
        view2.setNextFocusUpId(R.id.dial_tab);
        View view3 = getView().findViewById(R.id.input_key_number_3);
        view3.setTag(3);
        view3.setOnClickListener(this);
        view3.setNextFocusUpId(R.id.dial_tab);
        View view4 = getView().findViewById(R.id.input_key_number_4);
        view4.setTag(4);
        view4.setOnClickListener(this);
        View view5 = getView().findViewById(R.id.input_key_number_5);
        view5.setTag(5);
        view5.setOnClickListener(this);
        View view6 = getView().findViewById(R.id.input_key_number_6);
        view6.setTag(6);
        view6.setOnClickListener(this);
        View view7 = getView().findViewById(R.id.input_key_number_7);
        view7.setTag(7);
        view7.setOnClickListener(this);
        View view8 = getView().findViewById(R.id.input_key_number_8);
        view8.setTag(8);
        view8.setOnClickListener(this);
        View view9 = getView().findViewById(R.id.input_key_number_9);
        view9.setTag(9);
        view9.setOnClickListener(this);
        View viewDel = getView().findViewById(R.id.input_key_number_del);
        viewDel.setTag(-1);
        viewDel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int tag = (int) v.getTag();
        if (tag == -2) {
            dial();
        } else if (tag == -1) {// DEL
            delNumber();
        } else {
            inputNumber(tag);
        }
    }

    private void delNumber() {
        String text = showPhone.getText().toString();
        if (text != null && text.length() > 0) {
            text = text.substring(0, text.length() - 1);
            showPhone.setText(text);
        }
        dialBtnState(text);
    }

    private void inputNumber(int tag) {
        String text = showPhone.getText().toString();
        if (text == null) {
            text = new String(String.valueOf(tag));
        } else {
            text = text + tag;
        }
        dialBtnState(text);
        showPhone.setText(text);
    }

    private void dial() {
        String text = showPhone.getText().toString();
        int len = TextUtils.isEmpty(text) ? 0 : text.length();
        if (len != 11) {
            ToastUtil.showToast("你输入的账号不合法!");
            showPhone.setText("");
        } else {
            String uid = ContactProvider.getUidByPhone(text);
            if (TextUtils.isEmpty(uid)) {
                ToastUtil.showToast("该账号不存在!");
            } else {
                // TODO
            }
        }
    }

    private void dialBtnState(String text) {
        dialBnt.setEnabled(!TextUtils.isEmpty(text));
    }
}



网易云免费体验馆,0成本体验20+款云产品! 

更多网易研发、产品、运营经验分享请访问网易云社区


相关文章:
【推荐】 Vue框架核心之数据劫持
【推荐】 网易云首席安全架构师谈安全新形势:DDOS两三天,游戏玩家数从几万降到几百
【推荐】 abtest-system后台系统设计与搭建