今天看的书是android的第一行代码,就是自己按照书的进度,把代码复制了一点
学习了一点控件的用法,控件有:
1.TextView 文本框
2.Button 按键
3.EditView 编辑框
4.ImageView 图片
5.ProcessBar 进度条
6.AlertDialog 警告对话框
7.ProgressDialog 进度对话框
嗯,暂时就只知道这么一点
android控件使用规律(感觉写的不错):给控件定义一个id,指定一下控件的宽度和高度,之后再适当加入控件特有的一些属性就差不多了
对于目前不理解的问题,明天开一个单章。
1.手机上的闪退问题
2.对于本地图片分辨率的设置
3.从内部类中访问某个本地变量,需要被申明成本地类型。
下面复制一下当前所有的代码,写一下理解情况。
MainActivity :
package com.lib.myapplication;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements android.content.DialogInterface.OnClickListener {
@Override
public void onClick(final DialogInterface holder, final int position) { //这里是对接口的方法的重写,不然通不过。
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //这里开始调用了activity_main
Button button1 = (Button) findViewById(R.id.button); //对按键的调用
Button button2 = (Button) findViewById(R.id.button1);
Button button3 = (Button) findViewById(R.id.button2);
Button button4 = (Button) findViewById(R.id.button3);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
Button button7 = (Button) findViewById(R.id.button7);
final EditText editText = (EditText) findViewById(R.id.edit_text); //调用编辑框
final ImageView imageView = (ImageView) findViewById(R.id.image_view); //调用图像
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar); //进度条
final ProgressBar progressBar1 = (ProgressBar) findViewById(R.id.progress_bar1);
button1.setOnClickListener(new android.view.View.OnClickListener() { //按键的点击事件
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You clicked Button 1", //点击按键会短暂出现You clicked Button 1
Toast.LENGTH_SHORT).show();
}
});
button2.setOnClickListener(new android.view.View.OnClickListener() { //按键2的点击事件实现了警告对话框的出现
@Override
public void onClick(View v) {
android.support.v7.app.AlertDialog.Builder dialog = new android.support.v7.app.AlertDialog.Builder
(MainActivity.this);
dialog.setTitle("This is Dialog"); //警告对话框的标题
dialog.setMessage("Something important."); //对话框的提示信息
dialog.setCancelable(false); //设置是否可撤销
dialog.setPositiveButton("OK", new DialogInterface.
OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { //额,这里就是我前面说的对接口方法的重写
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.
OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show(); //点击后,显示对话框
}
});
button3.setOnClickListener(new android.view.View.OnClickListener() { //按键3是实现编辑框内容对重现
@Override
public void onClick(View v) {
String inputText = editText.getText().toString();
Toast.makeText(MainActivity.this, inputText,Toast.LENGTH_SHORT).show();
}
});
button4.setOnClickListener(new android.view.View.OnClickListener() { //实现了图像的切换
boolean flag=true;
@Override
public void onClick(View v) {
if(flag) {
imageView.setImageResource(R.mipmap.s2); s2是我自己新生成的图片
flag=false;
}
else if(!flag) {
imageView.setImageResource(R.mipmap.ic_launcher); //ic_launcher是另外一张图片
flag=true;
}
}
});
button5.setOnClickListener(new android.view.View.OnClickListener() { //进度条
@Override
public void onClick(View v) {
if (progressBar.getVisibility() == View.GONE) {
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.GONE);
}
}
});
button6.setOnClickListener(new android.view.View.OnClickListener() { //进度条的慢慢增加
@Override
public void onClick(View v) {
int progress = progressBar1.getProgress();
progress = progress + 10;
progressBar1.setProgress(progress);
}
});
button7.setOnClickListener(new android.view.View.OnClickListener() { //实现缓冲对话框
@Override
public void onClick(View v) {
ProgressDialog progressDialog = new ProgressDialog
(MainActivity.this);
progressDialog.setTitle("This is ProgressDialog");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
}
});
}
}
activity_main.xml :(主要内容都在这里编辑完成)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lib.myapplication.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="#0033ff"
android:gravity="center"
android:text="Hello World!"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Button 1" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="40"
android:maxLines="2"
android:textColorHint="#ff0011"
android:background="@drawable/bg_edittext" //这里就是对后面格式的引用
android:hint="在这里写一些东西"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 3" />
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 4" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 5" />
<ProgressBar
android:id="@+id/progress_bar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
/>
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 6" />
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 7" />
</LinearLayout>
下面这个是对文本框的一个比较简单的变化(当然不是我写的) :
be_edittext_normal.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<corners android:radius="10dip"/>
<stroke
android:width="1dip"
android:color="#00ff00" />
</shape>
be_edittext_focused.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dip"
android:color="#FF0800FF" />
</shape>
be_edittext.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="true"
android:drawable="@drawable/bg_edittext_normal" />
<item
android:state_focused="true"
android:drawable="@drawable/bg_edittext_focused" />
</selector>