Activity之间的跳转及数据传递
跳转 有两种显式 和 隐式
显示 就不用配置 文件里面写东西
隐式 需要写配置文件 但是它可以只要你的action 和 categories 对应上就可以调用
我下面的 显示写的方法 隐式写的监听器
然后显式的有intent传值 布局我就不粘了
下面是第一个主activity
package com.example.myapplication.Handler;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.myapplication.R;
public class MainActivity extends AppCompatActivity {
private Button btn=null,btn1 = null;
EditText name;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_layout);
btn=(Button)findViewById(R.id.call);
btn.setOnClickListener(listener);
btn1=findViewById(R.id.call1);
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.setAction("cn.itscast.call1");
startActivity(intent);
//回传数据
}
});
}
private View.OnClickListener listener=new View.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
name = findViewById(R.id.name);
String a = name.getText().toString();
System.out.println(a);
intent.putExtra("name", a);
startActivity(intent);//启动 intent
}
};
}
跳转到的第二个activity
package com.example.myapplication.Handler;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.example.myapplication.R;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView txt1=(TextView)findViewById(R.id.show);
Intent intent=getIntent();
String name=intent.getStringExtra("name");
txt1.setText(name);
}
}
你要觉得上面太low 你看这个 还有效果图
虽然依然不咋样 但是 是传过来了 传不过来 会返回错误信息 为了我另一个开发日记做铺垫了
先看看主页面activity(我加上import因为刚才引用错了 咋也调不出来)
package com.example.myapplication.Handler;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.myapplication.R;
import javax.xml.transform.Result;
public class LoginActivity extends AppCompatActivity {
EditText editText;
EditText editText1;
Button btnLogin;
Button btnReset;
TextView Error ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin=(Button)findViewById(R.id.btnLogin);
btnReset=(Button)findViewById(R.id.btnReset);
editText=(EditText)findViewById(R.id.editText);
editText1=(EditText)findViewById(R.id.editText1);
Error=(TextView)findViewById(R.id.textE);
btnReset.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//重置
editText.setText(null);
editText1.setText(null);
}
});
}
public void btnStartClick(View view){
String login = editText.getText().toString();
String password = editText1.getText().toString();
//存储帐号和密码的bundle
Bundle bundle=new Bundle();
bundle.putString("login", login);
bundle.putString("pwd", password);
//传到另一个activity
Intent intent = new Intent(LoginActivity.this, ResultActivity.class);
intent.putExtras(bundle);
startActivityForResult(intent,1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 1) {
Error.setText(data.getStringExtra("error"));
}
}
}
看看返回消息的activity
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView txt1=(TextView)findViewById(R.id.show);
Intent intent=getIntent();
String name=intent.getStringExtra("name");
txt1.setText(name);
}
}
欸有点儿可以
Handler
改了改之前写的不是很好的进度条 你们凑合着看
就是子线程里面写的 progress就是每次的进度
休息1秒钟的时间
别试运行一半更改 那会bug 但是 你要想突然改就得加监听器 别忘了
效果是这样的 我不会发动图 有没有人教教俺咋发
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/pgBar1"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:max="100"
android:progress="50"
/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:hint="在此处输入数字"
/>
<Button
android:id="@+id/btnStartClick"
android:onClick="btnStartClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"/>
</LinearLayout>
activity的代码:
package com.example.myapplication.Handler;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import com.example.myapplication.R;
public class MessageTestBarHandler extends AppCompatActivity {
ProgressBar pgBar1;
EditText editText;
Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
if(msg.what==101)
pgBar1.setProgress(msg.arg1);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_test_bar_handler);
pgBar1= (ProgressBar) findViewById(R.id.pgBar1);
editText=(EditText)findViewById(R.id.editText);
pgBar1.setProgress(0);
}
public void btnStartClick(View view){
System.out.println("start...");
new MyThread().start();
System.out.println("end");
}
public class MyThread extends Thread{
public void run(){
int count = Integer.parseInt(editText.getText().toString());
int progress = count;
int n = 100/count;
if(100%count>0) n = n + 1;
for(int i=0;i<n;i++){
Message msg=new Message();//每次发送,必须是单独的消息,不能定义一个消息,多次发送
msg.arg1=progress;
msg.what=101;
System.out.println("progress="+progress);
handler.sendMessage(msg);
progress=progress+count;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}