Android期末机试考核模拟题

1、创建一个Android的“Hello World”应用程序,用log.d()等方法打印一条调试信息。

package com.example.dell.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        Log.v("MainActivity", "Verbose");//打印所有信息
        Log.d("MainActivity", "Debug");//打印调试信息
        Log.i("MainActivity", "Info");//information、特定信息、提示性消息
        Log.w("MainActivity", "Warning");//警告
        Log.e("MainActivity", "Error");//错误
        Log.wtf("MainActivity", "Assert");
        //Log.println(, "MainActivity", "println");

    }

}

Android期末机试考核模拟题_第1张图片

2、给android工程添加一个字符串资源,并将资源内容显示在一个textview控件中。

Android期末机试考核模拟题_第2张图片



   
    


package com.example.dell.mylayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView)findViewById(R.id.textView);
        //getResources().getString(R.string.tv);
        textView.setText(getResources().getString(R.string.tv));
    }
}

Android期末机试考核模拟题_第3张图片

3、通过xml资源设置一个线性布局,并显示不少于三种控件内容,其中要求布局背景绿色,文字颜色红色。




    

    

Android期末机试考核模拟题_第4张图片

4、设置一种布局,要求屏幕左半部分背景白色、从右下角开始显示三个单选按钮,屏幕右半部分背景红色、居中水平显示两个复选框按钮。



    

    
        
            
            
            
        
    

    
        
        
            
            
        
    

Android期末机试考核模拟题_第5张图片

5、实现以下场景:从一个activity中点击一个按钮后,弹出一个对话框,对话框内容为“Hello Android”。

package com.example.dell.mylayout;

import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        //线性布局
        LinearLayout linearLayout = new LinearLayout(this);//创建LinearLayout对象
        //定义宽高
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        Button button = new Button(this);//创建Button控件
        button.setText("按钮");
        button.setTextSize(20);
        linearLayout.addView(button, params);//添加Button对象和Button的布局属性

        setContentView(linearLayout);//设置在Activity中显示LinearLayout

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }

    //对话框
    @Override
    public void onBackPressed(){
        //声明对象
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("普通对话框")                    //设置对话框标题
                .setIcon(R.mipmap.ic_launcher)             //设置标题图标
                .setMessage("Hellow Android")               //设置对话框提示信息,是否退出应用?
                //添加“确定”按钮
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();//关闭对话框
                        MainActivity.this.finish();
                    }
                })
                //添加“取消”按钮
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        dialog = builder.create();
        dialog.show();
    }
}

Android期末机试考核模拟题_第6张图片

6、使用java代码设置一种线性布局,布局背景颜色蓝色,并在布局中显示三个不同内容的按钮。

package com.example.dell.mylayout;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        //线性布局
        LinearLayout linearLayout = new LinearLayout(this);//创建LinearLayout对象
        //定义宽高
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        linearLayout.setBackgroundColor(Color.BLUE);//背景颜色

        Button button1 = new Button(this);//创建Button控件
        button1.setText("按钮1");
        button1.setTextSize(20);
        linearLayout.addView(button1, params);//添加Button对象和Button的布局属性

        Button button2 = new Button(this);
        button2.setText("按钮2");
        button2.setTextSize(20);
        linearLayout.addView(button2, params);

        Button button3 = new Button(this);
        button3.setText("我是按钮3");
        button3.setTextSize(22);
        linearLayout.addView(button3, params);

        setContentView(linearLayout);//设置在Activity中显示LinearLayout
    }
}

Android期末机试考核模拟题_第7张图片

7、设置一种绝对布局,要求右上角显示一个单选按钮,从左下角开始显示一个编辑框控件。




    

    

Android期末机试考核模拟题_第8张图片

8、实现数据从一个activity传输到另一个activity的android工程示例。

要求第一个activity布局为线性布局,布局包含若干控件;第二个activity布局形式不限,背景为蓝色。用户从第一个activity界面中输入数据,并点击确定按钮,然后跳转到第二个activity中,在第二activity中接收数据,并将数据居中显示在屏幕,且要求显示字体颜色绿色。




    
        
        


    
        
    

package com.example.dell.mylayout;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText et;
    Button bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et = (EditText)findViewById(R.id.et);
        bt = (Button)findViewById(R.id.bt);

        bt.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                String str = null;
                switch (view.getId()){
                    case R.id.bt:
                        str = String.valueOf(et.getText());
                        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                        intent.putExtra("str",str);
                        startActivity(intent);
                }
            }
        });
    }

}
package com.example.dell.mylayout;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Intent intent = this.getIntent();
        String str = intent.getStringExtra("str");
        TextView tv = (TextView)findViewById(R.id.tv);
        tv.setText(str);
    }
}

Android期末机试考核模拟题_第9张图片

Android期末机试考核模拟题_第10张图片

9、通过xml资源设置一个线性布局,并添加“发送有序广播”及“发送无序广播”两个按钮,点击按钮实现对应的功能。




    

Android期末机试考核模拟题_第11张图片

package com.example.dell.mylayout;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    MyBroadcastReceiverOne one;
    MyBroadcastReceiverTwo two;
    MyBroadcastReceiverThree three;
    MyBroadcastReceiver four;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linear_layout);

        registerReceiver();//注册广播接收者
        init();
    }

    private void registerReceiver(){
        //动态注册MyBroadcastReceiverOne广播
        one = new MyBroadcastReceiverOne();
        IntentFilter filter1 = new IntentFilter();
        filter1.setPriority(1000);//设置广播优先级
        filter1.addAction("Intercept_Stitch");
        registerReceiver(one, filter1);

        two = new MyBroadcastReceiverTwo();
        IntentFilter filter2 = new IntentFilter();
        filter2.setPriority(200);
        filter2.addAction("Intercept_Stitch");
        registerReceiver(two, filter2);

        three = new MyBroadcastReceiverThree();
        IntentFilter filter3 = new IntentFilter();
        filter3.setPriority(600);
        filter3.addAction("Intercept_Stitch");
        registerReceiver(three, filter3);

        four = new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("Intercept_Stitch0");
        registerReceiver(four, filter);
    }

    private void init(){
        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("Intercept_Stitch");//定义广播事件类型
                sendOrderedBroadcast(intent, null);//发送广播
            }
        });

        button2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent();
                intent.setAction("Intercept_Stitch0");
                sendBroadcast(intent);
            }
        });
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        unregisterReceiver(one);
        unregisterReceiver(two);
        unregisterReceiver(three);
        unregisterReceiver(four);
    }
}
package com.example.dell.mylayout;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent){
        Log.i("BroadcastReceiver", "无序广播,接收成功!");
    }
}
package com.example.dell.mylayout;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyBroadcastReceiverOne extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent){
        Log.i("BroadcastReceiver", "有序广播one,接收成功!");
    }
}

Android期末机试考核模拟题_第12张图片

Android期末机试考核模拟题_第13张图片

10、使用simpleAdapter实现ListView列表显示。

activity_main.xml 




    

list_item.xml



    
    
    

MainActivity.java

package com.example.dell.mylayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView)findViewById(R.id.lv);
        SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.list_item, new String[] { "title", "price" },new int[] { R.id.title, R.id.price });
        listView.setAdapter(adapter);
    }

    // 数据源的方法
    List> getData(){
        List> list = new ArrayList>();

        Map list_item = new HashMap();
        list_item.put("title", "苹果");
        list_item.put("price", "10元/kg");
        list.add(list_item);

        list_item = new HashMap();
        list_item.put("title", "荔枝");
        list_item.put("price", "10元/kg");
        list.add(list_item);

        list_item = new HashMap();
        list_item.put("title", "桃子");
        list_item.put("price", "10元/kg");
        list.add(list_item);

        return list;
    }
}

Android期末机试考核模拟题_第14张图片

你可能感兴趣的:(Android,编程练习)