Statements about Android development

Statements about Activity and layout in Android development

Basic statement in Activity

The design of the Android program pays attention to the separation of logic and view, and the layout is used to display the interface content

Sample layout file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"//活动的宽度
    android:layout_height="match_parent">//活动的高度

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1" />

</LinearLayout>

Toast Notification

Toast.makeText(FirstActivity.this,"提示信息",Toast.LENGTH_SHORT).show();

Button press starts activity

button1.setOnClickListener(new View.OnClickListener(){
    
    
   public void onClick(View v){
    
    
       //添加活动
   } 
});

Use the Menu in the event

1. Add menu folder and create main resource file

<item
	android:id="@+id/add_item"
	android:title="选项名称"/>

2. Override the onCreateOptionsMenu() method in the activity

public boolean onCreateOptionsMenu(Menu menu){
    
    
    getMenuInflater().inflate(R.menu.main,menu);
    return true;
}

3. Define the menu response event

public boolean onOptionsItemSelected(MenuItem item){
    
    
    switch (item.getItemId()){
    
    
        case R.id.“选项id”:
            //响应事件
            break;
    }
}

Navigate through an activity using an Intent

  • Explicit Intent
//点击Button后触发的操作
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
//点击Button后跳转到SecondActivity
  • Implicit Intent

1. Configure ACTION and CATEGORY in AndroidManifest.xml

<intent-filter>
	<action android:name="com.example.activitytest.ACTION_START"/>
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

2. Modify the click event in the activity

Intent intent = new Intent("在XML文件中配置的ACTION标签");
intent.addCategory("在XML文件中配置的CATEGORY标签");
startActivity(intent);

Statements related to UI

This part is configured in the layout file to set the Android interface

TextView

Used to display a piece of text information on the interface

<TextView
          android:id="@+id=/text_view"
          android:layout_width="match_parebt"
          android:layout_height="wrap_content"
          android:text="文本信息内容"
/>

Button

Important controls used by the program to interact with the user

<Button
        android:id="@id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮上的文字"
/>

EditText

The control used by the program to interact with the user, which allows the user to input and edit content in the control, and can process the content in the program.

<EditText
          android:id="+@id/edit_text"
          android:layout_width="match_parent"
          android:layout_height="wrap_conten"
          android:hint="Type something here"//提示性文字
          android:maxLines="3"//指定输入最大显示行数为3行
          />

Combination between Button and EditText

Define responsive activity in activity using this layout file

private EditText editText;
protected void onCreat(Bundle saveInstanceState){
    
    
    super.onCreate(saveInstanceState);
    setContentView(R.layout.activity_main);
    Button button=(Button)findViewById(R.id.button);//通过id找到按钮
    editText=(EditText)findViewById(R.id.edit_text);//通过id找到Edit的编辑框
    button.setOnClickListener(this);//通过点击Button触发事件
}
@override
public void onClick(View v){
    
    
    switch(v.getId()){
    
    
        case R.id.button:
            String inputText = editText.getText().toString();//将editText中的文字转换成String
            Toast.makeText(MainActivity.this,inputText,Toast.LENGTH_SHORT).show();//通过Toast消息提醒文本内容
            break;
        default:
            break;
    }
}

ImageView

ImageView is a control used to display pictures on the interface. The pictures are placed in the directory starting with "drawable". Since the preset drawable does not specify the resolution, it is necessary to create a "drawable-xhdpi" directory in the res directory. Among them xhdpi refers to the resolution of the picture

<ImageView
           android:id="@+id/image_view"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/img_1"//指明图片地址
           />

Also like the interaction between Button and EditText, you can also set the click button in the activity to change the picture

private ImageView imageView;
protected void onCreate(Bundle saveInstanceState){
    
    
    //....
    imageView = (ImageView)findviewById(R.id.image_view);
    //....
}
public void onClick(View v){
    
    
    switch(v.getId()){
    
    
            case R.id.button:
                imageView.setImageResource(R.drawable.img_2);
            break;
        default:
            break;            
    }
}

About the layout

LinearLayout (LinearLayout)

This layout will arrange the controls it contains in order in the linear direction. Linear includes horizontal linear and vertical linear. By modifying the android:orientation=" " in the layout file, the value in " " can be vertical (vertical linear ) and horizontal (horizontal linear)

  • android:layout_weight attribute

    This property allows us to specify the size of the control in a proportional way.

    //布局为水平线性
    <EditText
          ...
          android:layout_weight="1"
          ...
              />
    <Button
          ...
          android:layout_weight="1"
          ...
             />
    //此时两个控件在水平上平分宽度
    
Relative Layout (RelativeLayout)

Relative layout allows controls to appear anywhere in the layout through relative positioning.

The following label can control the absolute position of the control

android:layout_alignParentLeft=”true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"

The following labels can control the relative position between the controls

android:layout_above="@id/控件id名"//在某控件之上
android:layout_below="@id/控件id名"//在某控件之下
android:layout_toLeftof="@id/控件id名"//在某控件之左
android:layout_toRightof="@id/控件id名"//在某控件之右

Guess you like

Origin blog.csdn.net/weixin_45976751/article/details/116944671