这是一个小的Android Project,主要为了加深对MainActivity的理解。在简单开发的过程中遇到了以下几个problem:1)Android xml布局问题;2)程序未知bug报错;3)程序闪退问题。这篇博客将对遇到的几个问题的解决方法进行简单阐述。
程序源码
MainActivity.java:
public class MainActivity<setResult> extends AppCompatActivity {
private EditText main_edit1;
private EditText main_edit2;
private Button main_button1;
private Button main_button2;
private View.OnClickListener onClickListen= new View.OnClickListener() {
@Override
public void onClick(View view) {
if(view==main_button1)
{
Toast.makeText(MainActivity.this,"For Test1",0).show();
//创建一个隐式Intent
/*点击拨号时产生的ActivityManager记录
*flg=0x10200000 cmp=com.android.contacts/.activities.TwelveKeyDialer bnds=[202,2050][370,2218] (has extras)} from uid 10031
*/
String action=Intent.ACTION_DIAL;
Intent intent=new Intent(action);
String number=main_edit1.getText().toString();
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
}
else if(view==main_button2)
{
Toast.makeText(MainActivity.this,"For Test2",0).show();
//点击Button2进入短信编辑界面
Intent intent2=new Intent(Intent.ACTION_SENDTO);
String number=main_edit1.getText().toString();
String sms=main_edit2.getText().toString();
intent2.setData(Uri.parse("smsto:"+number));
intent2.putExtra("sms_body", sms);
startActivity(intent2);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化视图对象,相当于将MainActivity.java中的变量与xml中的视图对象关联
main_edit1 = findViewById(R.id.new_edit1);
main_edit2 = findViewById(R.id.new_edit2);
main_button1 = findViewById(R.id.new_button1);
main_button2 = findViewById(R.id.new_button2);
//给视图对象(button1和button2)设置监听
main_button1.setOnClickListener(onClickListen);
main_button2.setOnClickListener(onClickListen);
}
@Override
protected void onStart() {
Log.e("MES","This is a warning message");
super.onStart();
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/new_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Call"/>
<EditText
android:id="@+id/new_edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:hint="Phone Number"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="@+id/new_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Message" />
<EditText
android:id="@+id/new_edit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:hint="Message content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/new_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dip"
android:text="Function1" />
<Button
android:id="@+id/new_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dip"
android:text="Function2" />
</LinearLayout>
</LinearLayout>
界面布局
1)LinearLayout(线性布局):按照垂直的方向、或者是水平的方向来对其每一个视图。
2)RelativeLayout(相对布局): 较灵活,新加入一个控件时,指定其相对于上一个控件上下左右的边界的某一个位置,假如有一个视图控件的ID为A,还有一个视图控件ID为B,B可以设置为相对A的上、下、左、右的位置,相对的属性有 layout_above、layout_below、layout_toRightOf、layout_toRightOf 等等等。
3)ConstraintLayout(约束布局):貌似Android Studio的推荐布局,初次接触感觉有点难用。
下面是demo project的布局结构,主要采用的是垂直和水平的LinearLayout嵌套。感觉下次可以尝试一下RelativeLayout。
Intent的功能
Intent可以携带数据在Activity之间传播,是沟通Activity之间数据交互的bridge。
String action=Intent.ACTION_DIAL;
//public static final String ACTION_DIAL = "android.intent.action.DIAL";属于Intent.class中已经存在的
Intent intent=new Intent(action);
String number=main_edit1.getText().toString();//从EditText控件读入文本转换为字符串
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
MainActivity逻辑
MainActivity.java主要实现通过EditText控件接受用户输入、View.OnClickListener进行Button的触发监听、Intent传数据、startActivity()开始一个Activity。源码见MainActivity.java,在此不再赘述。