main xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD CONTACT"
android:id="@+id/btnadd"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="97dp"
android:onClick="addContact"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="VIEW CONTACT"
android:id="@+id/btnview"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="viewContact"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Network Found"
android:id="@+id/textview"
android:layout_below="@+id/btnview"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp" />
</RelativeLayout>
main java file
package com.example.dota.databasephp;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
Button btnadd,btnview;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnadd = (Button) findViewById(R.id.btnadd);
btnview = (Button) findViewById(R.id.btnview);
textView = (TextView) findViewById(R.id.textview);
btnadd.setOnClickListener(this);
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
{
textView.setVisibility(View.INVISIBLE);
}
else {
btnadd.setEnabled(false);
btnview.setEnabled(false);
}
}
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,Addinfo.class);
startActivity(i);
}
}
addinfo xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_addinfo"
tools:context="com.example.dota.databasephp.Addinfo">
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="@+id/et_name"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:text="Enter Name" />
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="@+id/et_email"
android:text="Enter Email"
android:layout_below="@+id/et_name"
android:layout_alignLeft="@+id/et_name"
android:layout_alignStart="@+id/et_name"
android:layout_marginTop="34dp"
android:inputType="textEmailAddress"/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="@+id/et_mobile"
android:text="Enter Mobile"
android:layout_below="@+id/et_email"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:inputType="phone"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAVE DATA"
android:id="@+id/button"
android:layout_below="@+id/et_mobile"
android:layout_centerHorizontal="true"
android:layout_marginTop="78dp"
android:onClick="saveInfo"/>
</RelativeLayout>
addinfo java file
package com.example.dota.databasephp;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class Addinfo extends Activity {
EditText Name,Email,Mobile;
String name,email,mobile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addinfo);
Name=(EditText)findViewById(R.id.et_name);
Email=(EditText)findViewById(R.id.et_email);
Mobile=(EditText)findViewById(R.id.et_mobile);
BackgroundTask backgroundTask=new BackgroundTask();
backgroundTask.execute(name,email,mobile);
finish();
}
public void saveInfo(View view)
{
name= Name.getText().toString();
email=Email.getText().toString();
mobile=Mobile.getText().toString();
}
class BackgroundTask extends AsyncTask<String, String, String>
{
String add_info_url;
@Override
protected void onPreExecute() {
add_info_url="Domain_name/add_info.php";
}
@Override
protected String doInBackground(String... params) {
String name,email,mobile;
name=params[0];
email=params[1];
mobile=params[2];
try {
URL url=new URL(add_info_url);
HttpURLConnection httpURLConnection= (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream outputStream=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data_string= URLEncoder.encode("name", "UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"="+
URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"="+
URLEncoder.encode("mobile","UTF-8")+"="+URLEncoder.encode(mobile,"UTF-8");
bufferedWriter.write(data_string);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream=httpURLConnection.getInputStream();
inputStream.close();
httpURLConnection.disconnect();
return "One row of data inserted";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
}
}
当我点击添加联系人按钮执行此代码后,会出现一个非常小的框。那个小盒子是什么?我没理解为什么这个盒子出现在执行中。请运行代码,您将了解问题所在。
答案 0 :(得分:0)
您正在Addinfo
中拨打finish()
来销毁onCreate()
个活动。
尝试删除此行:
finish();
来自您onCreate()
活动的Addinfo
。