[Java] android 字符串json对象转换 →→→→→进入此内容的聊天室

来自 , 2019-03-06, 写在 Java, 查看 148 次.
URL http://www.code666.cn/view/c15203a8
  1. //MainActivity.java
  2. //====================
  3. package com.v3;
  4.  
  5. import org.json.JSONObject;
  6.  
  7. import android.app.Activity;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14.  
  15. public class MainActivity extends Activity {
  16.         TextView Tname,TCountry,TDob,TCity;
  17.         EditText Ename,ECountry,EDob,ECity;
  18.         Button btnCreate;
  19.         String data="";
  20.     /** Called when the activity is first created. */
  21.     @Override
  22.     public void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.main);
  25.         btnCreate = (Button) findViewById(R.id.btnGen);
  26.         Tname = (TextView) findViewById(R.id.txtName);
  27.         Ename = (EditText) findViewById(R.id.editName);
  28.         TCity = (TextView) findViewById(R.id.txtCity);
  29.         ECity = (EditText) findViewById(R.id.editCity);
  30.         TCountry = (TextView) findViewById(R.id.txtCountry);
  31.         ECountry = (EditText) findViewById(R.id.editCountry);
  32.         TDob = (TextView) findViewById(R.id.txtDob);
  33.         EDob = (EditText) findViewById(R.id.editDob);
  34.  
  35.         btnCreate.setOnClickListener(new Button.OnClickListener()
  36.         {
  37.             public void onClick(View v)
  38.             {
  39.                 examineJSONFile();
  40.             }
  41.         });
  42.     }
  43.  
  44.     void examineJSONFile()
  45.     {
  46.         try
  47.         {
  48.             JSONObject object=new JSONObject();
  49.             object.put("Name", Ename.getText());
  50.             object.put("City", ECity.getText());
  51.             object.put("Country", ECountry.getText());
  52.             object.put("Dob", EDob.getText());
  53.                 Log.i("JsonString :", object.toString());
  54.  
  55.         }
  56.         catch (Exception je)
  57.         {
  58.  
  59.         }
  60.     }
  61. }
  62. //main.xml
  63. //============
  64. <?xml version="1.0" encoding="utf-8"?>
  65. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  66.     android:orientation="horizontal"
  67.     android:layout_width="fill_parent"
  68.     android:layout_height="fill_parent"
  69.     >
  70.     <TableLayout android:id="@+id/tableLayout1"
  71.                   android:layout_width="match_parent"
  72.                   android:layout_height="wrap_content"
  73.                    android:stretchColumns="1">
  74.         <TableRow >
  75.             <TextView
  76.                       android:text="Name"
  77.                       android:id="@+id/txtName"
  78.                       android:layout_width="wrap_content"
  79.                       android:layout_height="wrap_content"/>
  80.             <EditText android:text="" android:id="@+id/editName"
  81.                       android:layout_width="fill_parent"
  82.                       android:layout_height="wrap_content"/>
  83.         </TableRow>
  84.         <TableRow>
  85.         <TextView
  86.                       android:text="City"
  87.                       android:id="@+id/txtCity"
  88.                       android:layout_width="wrap_content"
  89.                       android:layout_height="wrap_content"/>
  90.             <EditText android:text="" android:id="@+id/editCity"
  91.                       android:layout_width="fill_parent"
  92.                       android:layout_height="wrap_content"/>
  93.          </TableRow>
  94.           <TableRow >            
  95.         <TextView
  96.                       android:text="Country :"
  97.                       android:id="@+id/txtCountry"
  98.                       android:layout_width="wrap_content"
  99.                       android:layout_height="wrap_content"/>
  100.             <EditText android:text="" android:id="@+id/editCountry"
  101.                       android:layout_width="fill_parent"
  102.                       android:layout_height="wrap_content"/>
  103.          </TableRow>
  104.           <TableRow >            
  105.             <TextView
  106.                       android:text="DOB"
  107.                       android:id="@+id/txtDob"
  108.                       android:layout_width="wrap_content"
  109.                       android:layout_height="wrap_content"/>
  110.             <EditText android:text="" android:id="@+id/editDob"
  111.                       android:layout_width="fill_parent"
  112.                       android:layout_height="wrap_content"/>
  113.  
  114.         </TableRow>
  115.           <TableRow >            
  116.            <Button android:text="Generate"
  117.             android:id="@+id/btnGen"
  118.             android:layout_width="wrap_content"
  119.             android:layout_height="wrap_content"/>
  120.  
  121.         </TableRow>
  122.     </TableLayout>
  123.  
  124. </LinearLayout>
  125. //java/79

回复 "android 字符串json对象转换"

这儿你可以回复上面这条便签

captcha