android programing 2.3.5
本帖最后由 alltolove 于 2017-9-19 08:02 编辑first_layout.xml里新添加个按钮和文本控件<Button
android:id="@+id/button_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button_4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text"
/>
把mainactivity.java文件修改为package com.example.xinwei.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
Button button1=(Button)findViewById(R.id.button_1);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "you clicked button1", Toast.LENGTH_SHORT).show();
}
});
Button button3=(Button)findViewById(R.id.button_3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("extra_data","hello activity1");
startActivity(intent);
}
});
Button button4=(Button)findViewById(R.id.button_4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent1,1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.add_item:
Toast.makeText(this, "You clicked add", Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this, "You clicked remove", Toast.LENGTH_SHORT).show();
break;
default:
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case 1:if(resultCode==RESULT_OK){
String result=data.getStringExtra("data_return");
TextView text =(TextView)findViewById(R.id.text);
text.setText(result);
}
break;
default:
}
}
void myClick(View v){
Toast.makeText(this, "这是我的方法", Toast.LENGTH_SHORT).show();
}
}
把secondactivity.java修改为:
package com.example.xinwei.test;
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.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button btn =(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1=getIntent();
intent1.putExtra("data_return","hello firstactivity");
setResult(RESULT_OK,intent1);
finish();
}
});
Button btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
startActivity(intent);
}
});
TextView text=(TextView)findViewById(R.id.text1);
Intent intent1=getIntent();
String string1 = intent1.getStringExtra("extra_data");
text.setText(string1);
}
}
效果图:
{:10_256:}{:10_256:}{:10_256:}不错 继续加油
页:
[1]