来福灵 發表於 2019-12-15 16:51:00

Android开发

<h2>环境安装</h2>
<ol>
<li>Android Studio安装版,解压版。需要配置jdk。</li>
<li>Android Studio配置包含SDK manager.、AVD manager、Adb。(都包含在sdk中)</li>
</ol>
<h2>项目目录</h2>
<p>新建项目后项目根目录如下。</p>
<p><img src="https://img2018.cnblogs.com/blog/1208477/201912/1208477-20191215163105031-504404331.png" alt=""></p>
<h3>AndroidManifest.xml</h3>
<p>项目AndroidManifest.xml配置文件配置项目的主activity,需要获取的权限等一些项目配置信息。</p>
<h3>java</h3>
<p>项目java目录下配置活动的后台代码。</p>
<h3>res</h3>
<p>项目res/layout目录下有activity的前端布局文件。</p>
<p>项目res/values目录下包含一些公用的配置。</p>
<ul>
<li>&nbsp; Drawable可以放置一些图片,但是不支持缩放。</li>
<li>&nbsp; colors.xml配置公共颜色;</li>
<li>&nbsp; dimens.xml配置组件大小等设置;</li>
<li>&nbsp; strings.xml配置公用字符串;</li>
<li>&nbsp; styles.xml配置公用样式。</li>
</ul>
<h2>Activity学习</h2>
<h3>dimens.xml</h3>
<p>配置组件通用大小以及组件字体大小等。</p>
<h3>string.xml</h3>
<p>配置通用字符串。</p>
<h3>activity.xml</h3>
<p><strong>LinearLayout</strong>两种<strong>LinearLayout</strong>控制整个页面的布局(横向和纵向)。</p>
<p><strong>match_parent</strong>代表高度/宽度匹配父级。</p>
<p><strong>wrap_content</strong>代表高度/宽度匹配内容。</p>
<p><strong>text</strong>引用String.xml中的内容。<strong>Text</strong><strong>中的内容可以在activity中写死也可以引用string.xml中配置,也可以在代码中设置。</strong></p>
<p><strong>gravity</strong>代表对齐方式。</p>
<p><strong>layout_weight </strong>代表控件占剩余控件的比例。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">TextView
    </span><span style="color: rgba(255, 0, 0, 1)">android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
    android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
    android:text</span><span style="color: rgba(0, 0, 255, 1)">="@string/txt_val"</span><span style="color: rgba(255, 0, 0, 1)">
    android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/txtCalc"</span><span style="color: rgba(255, 0, 0, 1)">
    android:textSize</span><span style="color: rgba(0, 0, 255, 1)">="28dp"</span><span style="color: rgba(255, 0, 0, 1)">
    android:gravity</span><span style="color: rgba(0, 0, 255, 1)">="right"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">Button
    </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/buttonequal"</span><span style="color: rgba(255, 0, 0, 1)">
    android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
    android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="@dimen/btn_size"</span><span style="color: rgba(255, 0, 0, 1)">
    android:textSize</span><span style="color: rgba(0, 0, 255, 1)">="@dimen/btn_fontsize"</span><span style="color: rgba(255, 0, 0, 1)">
    android:layout_weight</span><span style="color: rgba(0, 0, 255, 1)">="1"</span><span style="color: rgba(255, 0, 0, 1)">
    android:text</span><span style="color: rgba(0, 0, 255, 1)">="="</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span></pre>
</div>
<h3>Java代码控制组件</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">给文本框设置默认值</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">final</span> TextView txtCalc = (TextView)<span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.findViewById(R.id.txtCalc);
txtCalc.setText(</span>"0.0"<span style="color: rgba(0, 0, 0, 1)">);

Button button0 </span>= (Button)<span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.findViewById(R.id.button0);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义OnClickListener变量</span>
View.OnClickListener btnClickListener = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> View.OnClickListener(){
    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onClick(View v) {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">View代表被点击的控件</span>
<span style="color: rgba(0, 0, 0, 1)">      txtCalc.setText(((Button)v).getText() );
    }
};
button0.setOnClickListener(btnClickListener);</span></pre>
</div>
<h2>Intent</h2>
<h3>实现activity跳转</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">跳转到其他activity,参数一为本activity,参数二位要跳转到的activity的class</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
Intent intent </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Intent(MainActivity.<span style="color: rgba(0, 0, 255, 1)">this</span>,JumpActivity.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意这里一定只能存入String的value,第二个activity才能用getStringExtra()</span>
intent.putExtra("msg"<span style="color: rgba(0, 0, 0, 1)">,editText.getText().toString());
startActivity(intent);

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">第二个activity获取intent传输的值</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
Intent intent </span>= <span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.getIntent();
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">从intent的Extra中获取key为msg的字符串值</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
String msgVal </span>= intent.getStringExtra("msg");</pre>
</div>
<h3>调用浏览器</h3>
<div class="cnblogs_code">
<pre>Uri uri = Uri.parse("http://"+<span style="color: rgba(0, 0, 0, 1)">editText.getText());
Intent uri_intent </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Intent(Intent.ACTION_VIEW,uri);
startActivity(uri_intent);</span></pre>
</div>
<h3>调用电话</h3>
<div class="cnblogs_code">
<pre>&lt;!--电话权限--&gt;
&lt;uses-permission android:name="android.permission.CALL_PHONE"/&gt;

<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">调用电话</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
Uri teluri </span>= Uri.parse("tel:"+<span style="color: rgba(0, 0, 0, 1)">editText.getText());
Intent tel_intent </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Intent(Intent.ACTION_DIAL);
tel_intent.setData(teluri);
startActivity(tel_intent);</span></pre>
</div>
<h2>手机硬件调用</h2>
<h3>adb_interface_usb_driver</h3>
<p>调试手机用的usb驱动。一定要安装否则adb连接不上去。直接在设备管理器中双击黄色的感叹号,然后选择解压文件即可。</p>
<p>另外,红米note8 pro是可以在 开发者模式中 -&gt; 默认USB配置 -&gt; 选择USB网络共享,即可让电脑使用手机的流量(不经过FQ代理)。</p>
<h3>AndroidManifest.xml</h3>
<p>在这个文件中配置项目使用的权限,这里调用振动组件。</p>
<div class="cnblogs_code">
<pre>    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">获取振动权限</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.permission.VIBRATE"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">uses-permission</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<h3>activitey.xml</h3>
<p>在activity中,使用<strong>TextView</strong><strong>、</strong><strong>SeekBar</strong><strong>,</strong>其中TextView的值随着SeekBar的改动而改动。</p>
<p><strong>SeekBar</strong>要设置max值,也就是滑块的最大值。通过getProgress()方法获取SeekBar组件的值。</p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">TextView
            </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/txtView"</span><span style="color: rgba(255, 0, 0, 1)">
            android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
            android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
            android:text</span><span style="color: rgba(0, 0, 255, 1)">="New Text"</span><span style="color: rgba(255, 0, 0, 1)">
            android:layout_weight</span><span style="color: rgba(0, 0, 255, 1)">="1"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
      <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">滑动组件</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">SeekBar
            </span><span style="color: rgba(255, 0, 0, 1)">android:id</span><span style="color: rgba(0, 0, 255, 1)">="@+id/seekBar0"</span><span style="color: rgba(255, 0, 0, 1)">
            android:layout_width</span><span style="color: rgba(0, 0, 255, 1)">="match_parent"</span><span style="color: rgba(255, 0, 0, 1)">
            android:layout_height</span><span style="color: rgba(0, 0, 255, 1)">="wrap_content"</span><span style="color: rgba(255, 0, 0, 1)">
            android:layout_weight</span><span style="color: rgba(0, 0, 255, 1)">="1"</span><span style="color: rgba(255, 0, 0, 1)">
            android:max</span><span style="color: rgba(0, 0, 255, 1)">="2000"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span></pre>
</div>
<h3><span lang="EN-US">MainActivity.java</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">调用震动服务,Vibrator.class可以替换为Context.VIBRATOR_SERVICE</span>
Vibrator vibrator = getSystemService(Vibrator.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
vibrator.vibrate(</span>1000); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">振动一秒</span></pre>
</div>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    如果这篇文章对你有用,可以关注本人微信公众号获取更多ヽ(^ω^)ノ~<br>
<img id="ViewPicture1_GalleryImage" alt="微信公众号二维码" src="https://images.cnblogs.com/cnblogs_com/aeolian/1679458/o_wechat_gzh_qrcode.jpg" style="height: 258 px; width: 258 px; border-width: 0px">
<br><br><br>
来源:https://www.cnblogs.com/aeolian/p/12040807.html
頁: [1]
查看完整版本: Android开发