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");
}
}
或
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));
}
}
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();
}
}
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
}
}
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);
}
}
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,接收成功!");
}
}
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