第一个app--macconvert--获得的技能点
activity_main.xml --布局
1、layout布局或UI组件的参数设置
android:layout_margin ="10dp"边距设置
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" 方向设置-水平 --比如,如果是设置到linearlayout ,那么linearlayout中的view会 水平(横向) 地依次排列
android:orientation="vertical" 方向设置-纵向
2、UI组件的背景效果
---可以这样做(android:background=“@drawable/dr_edittext”),通过.xml文件来设置
<EditText android:id="@+id/edit_text1" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/dr_edittext" <-- dr_edittext.xml:shape的各种属性设置 --> android:hint="请输入.." />
这里推荐一个好用的工具:
3、Android Button Maker
Android Button Maker是一个在线生成Android应用按钮代码的工具。
Android的API提供可绘制资源,其中的XML文件定义的几何形状,包括颜色,边框和梯度。
这些按钮是在shape drawable XML代码基础上产生的相比通常的PNG按钮加载速度更快。
您可以在设置面板中自定义按钮的属性和获得源代码。
---> http://angrytools.com/android/button/
MainActivity.java--功能实现
package com.app.cceasy.macconvert; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.InputType; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.GridLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { GridLayout gridLayout; String [] strs = new String[]{ "0","1","2","3", "4","5","6","7", "8","9","A","B", "C","D","E","F" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridLayout = (GridLayout) findViewById(R.id.gridlt); EditText edit_text1 = (EditText) findViewById(R.id.edit_text1); EditText edit_text2 = (EditText) findViewById(R.id.edit_text2); EditText edit_text3 = (EditText) findViewById(R.id.edit_text3); edit_text1.setInputType(InputType.TYPE_NULL); //屏蔽软键盘弹出 edit_text2.setInputType(InputType.TYPE_NULL); //屏蔽软键盘弹出 edit_text3.setInputType(InputType.TYPE_NULL); //屏蔽软键盘弹出 for(int i=0;i<16;i++){ Button bn = new Button(this); bn.setText(strs[i]); bn.setId(i); bn.setTextSize(20); bn.setWidth(180); bn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText edit_text1 = (EditText) findViewById(R.id.edit_text1); EditText edit_text2 = (EditText) findViewById(R.id.edit_text2); EditText edit_text3 = (EditText) findViewById(R.id.edit_text3); Button b = (Button) findViewById(v.getId()); //判断是否有焦点,将键盘内容追加给已获得焦点的EditText --五星好评 if (edit_text1.isFocused()) { edit_text1.append(b.getText());//内容追加 edit_text1.setError(null);//清除错误消息框 } else if (edit_text2.isFocused()) { edit_text2.append(b.getText()); edit_text2.setError(null); } else if (edit_text3.isFocused()) { edit_text3.append(b.getText()); edit_text3.setError(null); } } }); GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(i/4),GridLayout.spec(i%4)); params.setGravity(Gravity.FILL); //将键盘体填入网格布局 gridLayout.addView(bn, params); } } public boolean getResult(View view){ EditText edit_text1 = (EditText) findViewById(R.id.edit_text1); EditText edit_text2 = (EditText) findViewById(R.id.edit_text2); EditText edit_text3 = (EditText) findViewById(R.id.edit_text3); TextView result = (TextView) findViewById(R.id.textview_macEnd); if("".equals(edit_text1.getText().toString())){ edit_text1.setError("请输入内容"); return false; }else if("".equals(edit_text2.getText().toString())){ edit_text2.setError("请输入内容"); return false; }else if("".equals(edit_text3.getText().toString())){ edit_text3.setError("请输入内容"); return false; } Long macStart = null; Long num1 = null; Long num2 = null; try{ macStart = Long.parseLong(edit_text1.getText().toString(),16); }catch (Exception e){ edit_text1.setError("输入有误"); return false; } try{ num1 = Long.parseLong(edit_text2.getText().toString(),10); }catch (Exception e){ edit_text2.setError("输入有误"); return false; } try{ num2 = Long.parseLong(edit_text3.getText().toString(),10); }catch (Exception e){ edit_text3.setError("输入有误"); return false; } result.setTextSize(20); result.setTextColor(Color.BLACK); result.setText("0x" + Long.toHexString(macStart+num1*num2-1)); //result.setText(edit_text1.getText().toString()+edit_text2.getText().toString()+edit_text3.getText().toString()); return true; } public void clearData(View view){ EditText edit_text1 = (EditText) findViewById(R.id.edit_text1); EditText edit_text2 = (EditText) findViewById(R.id.edit_text2); EditText edit_text3 = (EditText) findViewById(R.id.edit_text3); TextView result = (TextView) findViewById(R.id.textview_macEnd); edit_text1.setText(""); edit_text2.setText(""); edit_text3.setText(""); result.setText(""); edit_text1.setError(null); edit_text2.setError(null); edit_text3.setError(null); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
1、EditText.方法--功能用法
EditText:设置内容
edit_text1.setText("***");
EditText:追加内容
Button b = (Button) findViewById(v.getId());
edit_text1.append(b.getText());//内容追加
------------------------
EditText:屏蔽软键盘弹出
edit_text1.setInputType(InputType.TYPE_NULL);
-------------------------
EditText:冒出错误输入提示
if("".equals(edit_text1.getText().toString())){
edit_text1.setError("请输入内容"); //setError(“***”)方法:功能- 建立错误提示-冒出红色感叹号,并会在感叹号的下面弹出一个红色小文本框“请输入内容”
}
edit_text1.setError(null); //清除此输入框组件的错误提示标志
---------------------------------------
EditText:判断是否有焦点(是否正在被输入/被选取)
edit_text1.isFocused() //返回boolean值
---------------------------------
2、gridLayout:网格布局
Button bn = new Button(this);
.......对bn进行初始化,button的onClickListen初始化
//布局参数---控件处于网格布局的位置(行,列)
GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(i/4),GridLayout.spec(i%4));
//布局参数---fill
params.setGravity(Gravity.FILL);
//将键盘体填入网格布局
gridLayout.addView(bn, params);
gridLayout.addView(控件, 布局参数);
--------------------------------
3、从String对象中 获取各进制的整数
macStart = Long.parseLong(edit_text1.getText().toString(),16);
num1 = Long.parseLong(edit_text2.getText().toString(),10);
num2 = Long.parseLong(edit_text3.getText().toString(),10);
4、将整数转化为16进制的形式,再转成String对象,并返回此String对象
Long.toHexString(macStart+num1*num2-1)
drawable -----美化
(1)资源使用--在java中使用资源
Button bn = new Button(this);
bn.setBackgroundResource(R.drawable.keyboardbutton_shape);
bn.setBackgroundResource(R.drawable.keyboardbutton_shape);
(2)资源使用--在xml中使用资源
<Button
android:id="@+id/cleardata"
android:layout_width="80dp"
android:layout_height="40dp"
android:onClick="clearData"
android:background="@drawable/button_shape"
android:textSize="10pt"
android:textColor="@android:color/black"
android:text="重置" />
android:layout_width="80dp"
android:layout_height="40dp"
android:onClick="clearData"
android:background="@drawable/button_shape"
android:textSize="10pt"
android:textColor="@android:color/black"
android:text="重置" />
shape---文本框--填充、边框效果
button_shape.xml 输入框不同状态下(有焦点和没焦点)的不同显示效果选择
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:drawable="@drawable/contact_edit_edittext_normal" /> <item android:state_focused="true" android:drawable="@drawable/contact_edit_edittext_focused" /> </selector>
contact_edit_edittext_normal.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 填充 -->
<solid android:color="#FFFFFF" />
<!-- 圆角 -->
<corners android:radius="3dip"/>
<!-- 描边 -->
<stroke
android:width="1dip"
android:color="#961" />
<!-- 大小
<size
android:width="50dp"
android:height="50dp"/><!- 宽度和高度 -->
<!-- 文字和边框的间隔 -->
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp"/><!-- 各方向的间隔 -->
<!-- 渐变
<gradient
android:startColor="@android:color/white"
android:centerColor="@android:color/black"
android:endColor="@android:color/black"
android:useLevel="true"
android:angle="45"
android:type="radial"
android:centerX="0"
android:centerY="0"
android:gradientRadius="90"/>-->
</shape>