Android期末机试考核模拟题
16lz
2021-01-23
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"); }}
2、给android工程添加一个字符串资源,并将资源内容显示在一个textview控件中。
<?xml version="1.0" encoding="utf-8"?>
或
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)); }}
3、通过xml资源设置一个线性布局,并显示不少于三种控件内容,其中要求布局背景绿色,文字颜色红色。
<?xml version="1.0" encoding="utf-8"?>
4、设置一种布局,要求屏幕左半部分背景白色、从右下角开始显示三个单选按钮,屏幕右半部分背景红色、居中水平显示两个复选框按钮。
<?xml version="1.0" encoding="utf-8"?>
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(); }}
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 }}
7、设置一种绝对布局,要求右上角显示一个单选按钮,从左下角开始显示一个编辑框控件。
<?xml version="1.0" encoding="utf-8"?>
8、实现数据从一个activity传输到另一个activity的android工程示例。
要求第一个activity布局为线性布局,布局包含若干控件;第二个activity布局形式不限,背景为蓝色。用户从第一个activity界面中输入数据,并点击确定按钮,然后跳转到第二个activity中,在第二activity中接收数据,并将数据居中显示在屏幕,且要求显示字体颜色绿色。
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
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); }}
9、通过xml资源设置一个线性布局,并添加“发送有序广播”及“发送无序广播”两个按钮,点击按钮实现对应的功能。
<?xml version="1.0" encoding="utf-8"?>
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,接收成功!"); }}
10、使用simpleAdapter实现ListView列表显示。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
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
更多相关文章
- 开源库android-state-button 为Android打造的多状态按钮
- 详解Android读取本地图片和网络图片的方法
- Android中点击按钮的事件处理实现步骤
- android 浮动按钮
- Android 10 获取相册图片失败
- Android图片旋转实例
- android 相对布局
- 状态开关按钮ToggleButton
- Android之单选按钮对话框